UserController.php 14 KB

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