WechatUser.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/21
  6. */
  7. namespace app\models\user;
  8. use app\models\store\StoreCouponIssue;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\services\WechatService;
  11. use crmeb\traits\ModelTrait;
  12. use crmeb\services\SystemConfigService;
  13. use app\models\routine\RoutineQrcode;
  14. use app\models\store\StoreCouponUser;
  15. /**
  16. * TODO 微信用户Model
  17. * Class WechatUser
  18. * @package app\models\user
  19. */
  20. class WechatUser extends BaseModel
  21. {
  22. /**
  23. * 数据表主键
  24. * @var string
  25. */
  26. protected $pk = 'uid';
  27. /**
  28. * 模型名称
  29. * @var string
  30. */
  31. protected $name = 'wechat_user';
  32. use ModelTrait;
  33. public static function getOpenId($uid = '')
  34. {
  35. if ($uid == '') return false;
  36. return self::where('uid', $uid)->value('routine_openid');
  37. }
  38. /**
  39. * TODO 用uid获得openid
  40. * @param $uid
  41. * @param string $openidType
  42. * @return mixed
  43. * @throws \Exception
  44. */
  45. public static function uidToOpenid($uid, $openidType = 'routine_openid')
  46. {
  47. $openid = self::where('uid', $uid)->value($openidType);
  48. return $openid;
  49. }
  50. /**
  51. * TODO 用openid获得uid
  52. * @param $openid
  53. * @param string $openidType
  54. * @return mixed
  55. */
  56. public static function openidTouid($openid, $openidType = 'openid')
  57. {
  58. return self::where($openidType, $openid)->where('user_type', '<>', 'h5')->value('uid');
  59. }
  60. /**
  61. * 小程序创建用户后返回uid
  62. * @param $routineInfo
  63. * @return mixed
  64. */
  65. public static function routineOauth($routine)
  66. {
  67. $routineInfo['nickname'] = filter_emoji($routine['nickName']);//姓名
  68. $routineInfo['sex'] = $routine['gender'];//性别
  69. $routineInfo['language'] = $routine['language'];//语言
  70. $routineInfo['city'] = $routine['city'];//城市
  71. $routineInfo['province'] = $routine['province'];//省份
  72. $routineInfo['country'] = $routine['country'];//国家
  73. $routineInfo['headimgurl'] = $routine['avatarUrl'];//头像
  74. $routineInfo['routine_openid'] = $routine['openId'];//openid
  75. $routineInfo['session_key'] = $routine['session_key'];//会话密匙
  76. $routineInfo['unionid'] = $routine['unionId'];//用户在开放平台的唯一标识符
  77. $routineInfo['user_type'] = 'routine';//用户类型
  78. $routineInfo['mer_id'] = $routine['mer_id'];
  79. $spid = 0;//绑定关系uid
  80. $isCOde = false;
  81. //获取是否有扫码进小程序
  82. if ($routine['code']) {
  83. if ($info = RoutineQrcode::getRoutineQrcodeFindType($routine['code'])) {
  84. $spid = $info['third_id'];
  85. $isCOde = true;
  86. } else
  87. $spid = $routine['spid'];
  88. } else if ($routine['spid'])
  89. $spid = $routine['spid'];
  90. // 判断unionid 存在根据unionid判断
  91. if ($routineInfo['unionid'] != '' && ($uid = self::where(['unionid' => $routineInfo['unionid']])->where('user_type', '<>', 'h5')->value('uid'))) {
  92. self::edit($routineInfo, $uid, 'uid');
  93. $routineInfo['code'] = $spid;
  94. $routineInfo['isPromoter'] = $isCOde;
  95. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  96. User::updateWechatUser($routineInfo, $uid);
  97. } else if ($uid = self::where(['routine_openid' => $routineInfo['routine_openid']])->where('user_type', '<>', 'h5')->value('uid')) { //根据小程序openid判断
  98. self::edit($routineInfo, $uid, 'uid');
  99. $routineInfo['code'] = $spid;
  100. $routineInfo['isPromoter'] = $isCOde;
  101. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  102. User::updateWechatUser($routineInfo, $uid);
  103. } else {
  104. $routineInfo['add_time'] = time();//用户添加时间
  105. $routineInfo['mer_id'] = $routine['mer_id'];//用户添加商户
  106. $routineInfo = self::create($routineInfo);
  107. $res = User::setRoutineUser($routineInfo, $spid);
  108. $uid = $res->uid;
  109. }
  110. return $uid;
  111. }
  112. /**
  113. * 判断是否是小程序用户
  114. * @param int $uid
  115. * @return bool|int|string
  116. */
  117. public static function isRoutineUser($uid = 0)
  118. {
  119. if (!$uid) return false;
  120. return self::where('uid', $uid)->where('user_type', 'routine')->count();
  121. }
  122. /**
  123. * @param int $uid
  124. * @return int
  125. */
  126. public static function isUserStatus($uid = 0)
  127. {
  128. if (!$uid) return 0;
  129. $user = User::getUserInfo($uid);
  130. return $user['status'];
  131. }
  132. /**
  133. * .添加新用户
  134. * @param $openid
  135. * @return object
  136. */
  137. public static function setNewUser($openid)
  138. {
  139. $userInfo = WechatService::getUserInfo($openid);
  140. if (!isset($userInfo['subscribe']) || !$userInfo['subscribe'] || !isset($userInfo['openid']))
  141. exception('请关注公众号!');
  142. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  143. //判断 unionid 是否存在
  144. if (isset($userInfo['unionid'])) {
  145. $wechatInfo = self::where('unionid', $userInfo['unionid'])->find();
  146. if ($wechatInfo) {
  147. return self::edit($userInfo, $userInfo['unionid'], 'unionid');
  148. }
  149. }
  150. self::beginTrans();
  151. $wechatUser = self::create(is_object($userInfo) ? $userInfo->toArray() : $userInfo);
  152. if (!$wechatUser) {
  153. self::rollbackTrans();
  154. exception('用户储存失败!');
  155. }
  156. if (!User::setWechatUser($wechatUser)) {
  157. self::rollbackTrans();
  158. exception('用户信息储存失败!');
  159. }
  160. self::commitTrans();
  161. self::userFirstSubGiveCoupon($openid);
  162. return $wechatUser;
  163. }
  164. /**
  165. * TODO 关注送优惠券
  166. * @param $openid
  167. */
  168. public static function userFirstSubGiveCoupon($openid)
  169. {
  170. $couponIds = StoreCouponIssue::where('status', 1)
  171. ->where('is_give_subscribe', 1)
  172. ->where('is_del', 0)
  173. ->column('cid');
  174. if ($couponIds)
  175. foreach ($couponIds as $couponId)
  176. if ($couponId)
  177. StoreCouponUser::addUserCoupon(self::openidToUid($openid), $couponId);
  178. }
  179. /**
  180. * 订单金额达到预设金额赠送优惠卷
  181. * @param $uid
  182. */
  183. public static function userTakeOrderGiveCoupon($uid, $total_price)
  184. {
  185. $couponIds = StoreCouponIssue::where('status', 1)
  186. ->where('is_full_give', 1)
  187. ->where('is_del', 0)
  188. ->column('cid,full_reduction');
  189. if ($couponIds)
  190. foreach ($couponIds as $couponId)
  191. if ($couponId)
  192. if ($total_price >= $couponId['full_reduction'])
  193. StoreCouponUser::addUserCoupon($uid, $couponId['cid']);
  194. }
  195. /**
  196. * 更新用户信息
  197. * @param $openid
  198. * @return bool
  199. */
  200. public static function updateUser($openid)
  201. {
  202. $userInfo = WechatService::getUserInfo($openid);
  203. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  204. return self::edit($userInfo->toArray(), $openid, 'openid');
  205. }
  206. /**
  207. * 用户存在就更新 不存在就添加
  208. * @param $openid
  209. */
  210. public static function saveUser($openid)
  211. {
  212. self::be($openid, 'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
  213. }
  214. /**
  215. * 用户取消关注
  216. * @param $openid
  217. * @return bool
  218. */
  219. public static function unSubscribe($openid)
  220. {
  221. return self::edit(['subscribe' => 0], $openid, 'openid');
  222. }
  223. /**
  224. * TODO 用uid获得Unionid
  225. * @param $uid
  226. * @return mixed
  227. */
  228. public static function uidToUnionid($uid)
  229. {
  230. return self::where('uid', $uid)->value('unionid');
  231. }
  232. /**
  233. * TODO 获取用户信息
  234. * @param $openid
  235. * @param $openidType
  236. * @return array
  237. * @throws \think\db\exception\DataNotFoundException
  238. * @throws \think\db\exception\ModelNotFoundException
  239. * @throws \think\exception\DbException
  240. */
  241. public static function getWechatInfo($openid, $openidType)
  242. {
  243. if (is_numeric($openid)) $openid = self::uidToOpenid($openid);
  244. $wechatInfo = self::where($openidType, $openid)->find();
  245. if (!$wechatInfo) {
  246. self::setNewUser($openid);
  247. $wechatInfo = self::where($openidType, $openid)->find();
  248. }
  249. if (!$wechatInfo) exception('获取用户信息失败!');
  250. return $wechatInfo->toArray();
  251. }
  252. }