wxusermodel.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class wxusermodel extends Model
  5. {
  6. protected $table = "t_wxuser";
  7. public function insertData($data)
  8. {
  9. if (!isset($data['create_date']) || empty($data['create_date']) || !is_string($data['create_date'])) {
  10. $data['create_date'] = date("Y-m-d H:i:s");
  11. }
  12. if (isset($data['id'])) {
  13. unset($data['id']);
  14. }
  15. $data = $this->formatData($data);
  16. if (empty($data)) {
  17. return false;
  18. }
  19. $id = $this->insertGetId($data);
  20. return $id ? $id : false;
  21. }
  22. /**
  23. * 校验入库数据
  24. * 20220119
  25. */
  26. private function formatData($data)
  27. {
  28. $useData = [];
  29. $fields = $this->getTableFields();
  30. foreach ($data as $key => $value) {
  31. if (in_array($key, $fields)) {
  32. $useData[$key] = $value;
  33. }
  34. }
  35. return $useData;
  36. }
  37. public function getInfo($where, $field = "*", $row = true)
  38. {
  39. $info = $this->field($field)->where($where);
  40. if ($row) {
  41. $info = $info->find();
  42. } else {
  43. $info = $info->select();
  44. }
  45. return empty($info) ? false : $info;
  46. }
  47. public function updateinfo($where, $updateData)
  48. {
  49. $row = $this->where($where)->update($updateData);
  50. return empty($row) ? false : $row;
  51. }
  52. public function deleteinfo($where)
  53. {
  54. $row = $this->where($where)->delete();
  55. return empty($row) ? false : $row;
  56. }
  57. public function getList($where = [], $field = "*", $page = 1, $size = 10, $order = "id desc", $group = "", $row = false)
  58. {
  59. $sqlObj = $this->where($where);
  60. if ("count" != $field) {
  61. $sqlObj = $sqlObj->field($field)->order($order)->group($group)->page($page, $size);
  62. if ($row) {
  63. $data = $sqlObj->find();
  64. } else {
  65. $data = $sqlObj->select();
  66. }
  67. } else {
  68. $data = $sqlObj = $sqlObj->count();
  69. }
  70. return $data;
  71. }
  72. /**
  73. * 根据id获取信息
  74. * wj
  75. * 20220118
  76. */
  77. public function getinfobyid($id, $field = false)
  78. {
  79. $defaultfield = ['id', 'open_id', 'union_id', 'nick_name', 'avatar_url', 'gender', 'province', 'city', 'country', 'tel', 'address', 'create_date', 'real_name'];
  80. $field = $field ? $field : $defaultfield;
  81. $where = ['id' => $id];
  82. $info = $this->where($where)->$field()->find();
  83. return $info;
  84. }
  85. /**
  86. * 根据id修改数据
  87. * wj
  88. * 20220118
  89. */
  90. public function updatebyid($id, $updateData)
  91. {
  92. $where = ['id' => $id];
  93. $updateData = $this->formatData($updateData);
  94. if (empty($updateData)) {
  95. return false;
  96. }
  97. $row = $this->where($where)->update($updateData);
  98. return $row;
  99. }
  100. /**
  101. * 根据openid修改unionid
  102. *
  103. * @param [type] $openid
  104. * @return void
  105. * @author wj
  106. * @date 2022-07-22
  107. */
  108. public function updateunionidbyopenid($openid, $unionid)
  109. {
  110. $where = ['openid' => $openid];
  111. $info = $this->where($where)->find();
  112. if (!empty($info)) {
  113. $id = $info['id'];
  114. if ($info['unionid'] != $unionid) {
  115. $updateData = ['unionid' => $unionid];
  116. $row = $this->where(['id' => $id])->update($updateData);
  117. if (empty($row)) {
  118. return backarr(0, '用户unionid修改失败');
  119. }
  120. }
  121. }
  122. return backarr(1, '操作成功');
  123. }
  124. /**
  125. * 根据openid获取数据
  126. *
  127. * @param [type] $openid
  128. * @return void
  129. * @author wj
  130. * @date 2022-07-22
  131. */
  132. public function getinfobyopenid($openid, $field = false)
  133. {
  134. $defaultfield = ['id', 'open_id', 'union_id', 'nick_name', 'avatar_url', 'gender', 'province', 'city', 'country', 'tel', 'address', 'create_date', 'real_name'];
  135. $field = $field ? $field : $defaultfield;
  136. $where = ['openid' => $openid];
  137. $info = $this->where($where)->field($field)->find();
  138. return $info ? $info : false;
  139. }
  140. }