wxusermodel.php 4.1 KB

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