WechatUser.php 8.7 KB

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