UserController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. namespace mall\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use bizHd\custom\classes\CustomClass;
  5. use bizHd\wx\classes\WxOpenClass;
  6. use bizMall\message\services\SmsService;
  7. use bizMall\user\classes\UserClass;
  8. use bizMall\user\services\UserAssetService;
  9. use bizMall\user\services\UserService;
  10. use biz\shop\classes\ShopClass;
  11. use common\components\dict;
  12. use common\components\imgUtil;
  13. use common\components\jwt;
  14. use common\components\noticeUtil;
  15. use common\components\stringUtil;
  16. use Yii;
  17. use common\components\util;
  18. use yii\helpers\Json;
  19. use Overtrue\ChineseCalendar\Calendar;
  20. class UserController extends BaseController
  21. {
  22. //二维码收款使用 ssh
  23. public $guestAccess = ['discount', 'mini-mobile', 'create', 'account-login', 'current-info'];
  24. //清空登录信息 ssh 20250215
  25. public function actionClearLogin()
  26. {
  27. $user = $this->user;
  28. $user->miniOpenId = '';
  29. $user->save();
  30. util::complete();
  31. }
  32. //修改信息 ssh 20221015
  33. public function actionUpdate()
  34. {
  35. $post = Yii::$app->request->post();
  36. $user = $this->user;
  37. if (empty($user)) {
  38. util::fail('请先登录');
  39. }
  40. $userId = $user->id ?? 0;
  41. $address = $post['address'] ?? '';
  42. $floor = $post['floor'] ?? '';
  43. $fullAddress = $address . $floor;
  44. $post['fullAddress'] = $fullAddress;
  45. UserClass::updateUserInfo($user, $post);
  46. util::complete();
  47. }
  48. //修改生日 ssh 20250830
  49. public function actionModifyBirthday()
  50. {
  51. date_default_timezone_set('PRC');
  52. $get = Yii::$app->request->get();
  53. $lunar = $get['lunar'] ?? 0;
  54. $birthdayMonth = $get['birthdayMonth'] ?? 1;
  55. if ($birthdayMonth > 12 || $birthdayMonth < 1) {
  56. util::fail('月份必须1到12月');
  57. }
  58. $birthdayDate = $get['birthdayDate'] ?? 1;
  59. if ($birthdayDate > 31 || $birthdayDate < 1) {
  60. util::fail('日期必须1到31号');
  61. }
  62. if ($lunar == 1 && $birthdayDate == 31) {
  63. util::fail('农历没有31号');
  64. }
  65. $user = $this->user;
  66. $userId = $this->userId;
  67. if ($user->birthdaySet == 1) {
  68. util::fail('你已经设置过了');
  69. }
  70. if ($lunar == 1) {
  71. try {
  72. //农历转阳历,有多处使用,需要同步修改,关键词change_my_birthday
  73. $calendar = new Calendar();
  74. $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
  75. $month = $result['gregorian_month'] ?? 1;
  76. $date = $result['gregorian_day'] ?? 1;
  77. $birthday = date("Y") . '-' . $month . '-' . $date;
  78. } catch (\InvalidArgumentException $e) {
  79. $msg = $e->getMessage();
  80. $birthday = '0000-00-00 00:00:00';
  81. util::fail($msg);
  82. }
  83. } else {
  84. $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
  85. }
  86. $birthdayTime = strtotime($birthday);
  87. $user->lunar = $lunar;
  88. $user->birthdayDate = $birthdayDate;
  89. $user->birthdayMonth = $birthdayMonth;
  90. $user->birthday = $birthday;
  91. $user->birthdayTime = $birthdayTime;
  92. $user->birthdaySet = 1;
  93. $user->save();
  94. $userId = $user->id;
  95. $customList = \bizMall\custom\classes\CustomClass::getAllByCondition(['userId' => $userId], null, '*', null, true);
  96. if (!empty($customList)) {
  97. foreach ($customList as $custom) {
  98. $custom->lunar = $lunar;
  99. $custom->birthdayDate = $birthdayDate;
  100. $custom->birthdayMonth = $birthdayMonth;
  101. $custom->birthday = $birthday;
  102. $custom->birthdayTime = $birthdayTime;
  103. $custom->save();
  104. }
  105. }
  106. util::complete('修改成功');
  107. }
  108. //获取当前登录用户的信息 ssh 20221014
  109. public function actionCurrentInfo()
  110. {
  111. $user = $this->user;
  112. if (!empty($user)) {
  113. $birthday = $user->birthday ?? '';
  114. //如果当年的生日时间没有更新,则需要给更新
  115. $year = date("Y", strtotime($birthday));
  116. $thisYear = date("Y");
  117. if ($year != $thisYear) {
  118. $lunar = $user->lunar ?? 0;
  119. $userId = $this->userId;
  120. $birthdayMonth = $user->birthdayMonth ?? 0;
  121. $birthdayDate = $user->birthdayDate ?? 0;
  122. if ($lunar == 1 && $birthdayMonth > 0 && $birthdayDate > 0) {
  123. try {
  124. //农历转阳历,有多处使用,需要同步修改,关键词change_my_birthday
  125. $calendar = new Calendar();
  126. $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
  127. $month = $result['gregorian_month'] ?? 1;
  128. $date = $result['gregorian_day'] ?? 1;
  129. $birthday = date("Y") . '-' . $month . '-' . $date;
  130. $user->birthday = $birthday;
  131. $user->save();
  132. CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
  133. } catch (\InvalidArgumentException $e) {
  134. $msg = $e->getMessage();
  135. noticeUtil::push("更新生日出错了呢,userId:" . $userId . ' ' . $msg, '15280215347');
  136. }
  137. } else {
  138. $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
  139. $user->birthday = $birthday;
  140. $user->save();
  141. CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
  142. }
  143. }
  144. // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  145. $user = $user->toArray();
  146. $shortAvatar = $user['avatar'] ?? '';
  147. $smallAvatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  148. $avatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  149. $user['avatar'] = $avatar;
  150. $user['smallAvatar'] = $smallAvatar;
  151. $user['shortAvatar'] = $shortAvatar;
  152. }
  153. // 补充店铺信息
  154. $shop = [];
  155. if (!empty($this->shop)) {
  156. $intraCityRet = ShopClass::hasIntraCity($this->shop);
  157. $shop['openIntraCity'] = $intraCityRet['openIntraCity'];
  158. $shop['hcFreeKm'] = $intraCityRet['hcFreeKm'];
  159. $shop['hcMap'] = $intraCityRet['hcMap'];
  160. }
  161. util::success(['info' => $user, 'shop' => $shop]);
  162. }
  163. //使用账号登录 ssh 20220920
  164. public function actionAccountLogin()
  165. {
  166. $getParams = Yii::$app->request->get();
  167. $postParams = Yii::$app->request->post();
  168. $allParams = array_merge($getParams, $postParams);
  169. $mobile = $allParams['mobile'] ?? '';
  170. $password = $allParams['password'] ?? '';
  171. if ($password != 'a.1234') {
  172. util::fail('密码错误');
  173. }
  174. $user = UserClass::getByCondition(['mobile' => $mobile], true);
  175. if (empty($user)) {
  176. util::fail('账号无效');
  177. }
  178. $userId = $user->id ?? 0;
  179. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  180. // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  181. $user = $user->toArray();
  182. $user['hasMap'] = dict::getDict('hasMap');
  183. util::success(['user' => $user, 'token' => $token]);
  184. }
  185. //生成用户和花店增加客户 ssh 20250307
  186. public function actionCreate()
  187. {
  188. $post = Yii::$app->request->post();
  189. $user = UserClass::replaceUser($post);
  190. if (!empty($this->shop)) {
  191. //花店增加客户
  192. CustomClass::buildRelation($this->shop, $user);
  193. }
  194. $userId = $user->id ?? 0;
  195. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  196. $fullUser = UserClass::getById($userId, true);
  197. // 将 $fullUser 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  198. $fullUser = $fullUser->toArray();
  199. $fullUser['hasMap'] = dict::getDict('hasMap');
  200. util::success(['user' => $fullUser, 'token' => $token]);
  201. }
  202. //最近店铺 ssh 2021.2.27
  203. public function actionRecentShop()
  204. {
  205. $list = [];
  206. //显示国兰的信息 0不是 1是
  207. $showGlInfo = getenv('SHOW_GL_INFO');
  208. $info = $showGlInfo === false ? 1 : $showGlInfo;
  209. util::success(['list' => $list, 'showGlInfo' => $info]);
  210. }
  211. //获取登陆客户的资产 ssh 2019.11.22
  212. public function actionAsset()
  213. {
  214. $asset = UserAssetService::getByUserId($this->userId);
  215. util::success($asset);
  216. }
  217. //获取登陆客户的优惠情况 ssh 2019.12.3
  218. public function actionDiscount()
  219. {
  220. $discount = UserAssetService::getDiscount($this->userId);
  221. util::success($discount);
  222. }
  223. //小程序用户完整信息 ssh 2019.12.12
  224. public function actionMiniFullInfo()
  225. {
  226. $post = Yii::$app->request->post();
  227. $iv = $post['iv'] ?? '';
  228. $encryptedData = $post['encryptedData'] ?? '';
  229. $merchant = WxOpenClass::getMallWxMiniBase();
  230. $appId = $merchant['miniAppId'];
  231. $miniOpenId = $post['miniOpenId'];
  232. if (empty($miniOpenId)) {
  233. util::success(['hasNoOpenId' => 1], '登录失败');
  234. }
  235. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  236. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  237. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  238. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  239. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  240. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  241. if ($errCode != 0) {
  242. Yii::info($result . ' ' . $errCode);
  243. util::fail('获取用户信息失败');
  244. }
  245. Yii::info('小程序获取用户信息:' . $result);
  246. $originalInfo = Json::decode($result);
  247. util::success($originalInfo);
  248. }
  249. //小程序用户手机号
  250. public function actionMiniMobile()
  251. {
  252. $post = Yii::$app->request->post();
  253. $iv = $post['iv'];
  254. $encryptedData = $post['encryptedData'];
  255. $merchant = WxOpenClass::getMallWxMiniBase();
  256. $appId = $merchant['miniAppId'];
  257. $miniOpenId = $post['miniOpenId'];
  258. if (empty($miniOpenId)) {
  259. util::success(['hasNoOpenId' => 1], '登录失败');
  260. }
  261. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  262. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  263. if (empty($sessionKey)) {
  264. util::fail('系统错误,session empty');
  265. }
  266. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  267. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  268. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  269. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  270. if ($errCode != 0) {
  271. util::fail('解密失败');
  272. }
  273. $arr = Json::decode($result);
  274. $mobile = $arr['purePhoneNumber'] ?? '';
  275. if (empty($mobile)) {
  276. util::fail('没有取到手机号,请重试');
  277. }
  278. util::success(['mobile' => $mobile]);
  279. }
  280. //密码修改
  281. public function actionPassword()
  282. {
  283. $post = Yii::$app->request->post();
  284. $old = $post['old'] ?? '';
  285. $new = $post['new'] ?? '';
  286. $confirm = $post['confirm'] ?? '';
  287. if (empty($new)) {
  288. util::fail('请输入新密码');
  289. }
  290. if ($new != $confirm) {
  291. util::fail('二次密码不一致');
  292. }
  293. $userId = $this->userId;
  294. $user = UserService::getUserInfo($userId);
  295. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  296. if (empty($old)) {
  297. util::fail('请填写旧密码');
  298. }
  299. if (!password_verify($old, $user['payPassword'])) {
  300. util::fail('旧密码错误');
  301. }
  302. }
  303. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  304. util::complete('修改成功');
  305. }
  306. //申请会员 ssh 2019.12.18
  307. public function actionApplyMember()
  308. {
  309. $post = Yii::$app->request->post();
  310. $mobile = $post['mobile'] ?? 0;
  311. if (!stringUtil::isMobile($mobile)) {
  312. util::fail('请输入手机号');
  313. }
  314. $userId = $this->userId;
  315. $asset = UserAssetService::getByUserId($userId);
  316. if (isset($asset['member']) && $asset['member'] != -1) {
  317. util::fail('已经是会员了');
  318. }
  319. $code = $post['code'] ?? '';
  320. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  321. if (empty($cacheCode) || $cacheCode != $code) {
  322. util::fail('验证码错误');
  323. }
  324. $update = [];
  325. if (!empty($post['realName'])) {
  326. $update['realName'] = $post['realName'];
  327. }
  328. if (!empty($post['birthday'])) {
  329. $update['birthday'] = $post['birthday'];
  330. }
  331. $user = UserService::getByCondition(['sjId' => $this->sjId, 'mobile' => $mobile]);
  332. if ($userId == $user['id']) {
  333. util::fail('手机号已验证');
  334. }
  335. if (!empty($user)) {
  336. util::fail('手机号已经注册过了');
  337. }
  338. $update['mobile'] = $mobile;
  339. $userInfo = $this->user->attributes ?? [];
  340. UserService::becomeMember($userInfo, $this->sj->attributes, $update);
  341. util::complete('注册成功');
  342. }
  343. //登陆客户的信息 ssh 2020.3.12
  344. public function actionLoginDetail()
  345. {
  346. $user = $this->user->attributes ?? [];
  347. util::success($user);
  348. }
  349. }