Account.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. namespace app\abutment\logic;
  3. use app\abutment\model\SupplierUser as SupplierUserModel;
  4. use app\abutment\model\SupplierRelationUser as SupplierRelationUserModel;
  5. use app\abutment\model\SupplierUser;
  6. use think\Exception;
  7. use think\facade\Cache;
  8. use think\facade\Config;
  9. use think\facade\Db;
  10. use think\helper\Str;
  11. //供应商账号管理
  12. class Account
  13. {
  14. //登录
  15. public static function login(array $param = [])
  16. {
  17. $db = new SupplierUserModel();
  18. $res = $db->where([
  19. 'is_del' => $db::$is_del_normal,
  20. 'mobile' => $param['mobile']
  21. ])
  22. ->findOrEmpty()
  23. ->toArray();
  24. if (empty($res)) return json_show(1004, '该账号不存在');
  25. if ($res['status'] == $db::$status_disabled) return json_show(1004, '该账号已禁用');
  26. $password = get_encryption_password($param['password'], $res['salt']);
  27. if ($password['password'] != $res['password']) return json_show(1004, '密码错误');
  28. //更新token
  29. $token = Str::random(40, -1);
  30. $expire_int = mt_rand(3600, 7200);
  31. $expire_time = date('Y-m-d H:i:s', time() + $expire_int);
  32. $rs = $db
  33. ->where($db->getPk(), $res[$db->getPk()])
  34. ->save(['token' => $token, 'expire_time' => $expire_time]);
  35. if (!$rs) throw new Exception('更新账号token信息失败');
  36. $list = SupplierRelationUserModel::field('id,supplierNo,supplierName')
  37. ->where(['is_del' => $db::$is_del_normal,'uid' => $res['uid']])
  38. ->select()
  39. ->toArray();
  40. //获取这些供应商的状态
  41. $status = Db::name('supplier')
  42. ->where('is_del',0)
  43. ->whereIn('code',array_column($list,'supplierNo'))
  44. ->column('status','code');
  45. foreach ($list as &$value){
  46. $value['status'] = $status[$value['supplierNo']];
  47. }
  48. $info = [
  49. 'uid' => $res['uid'],
  50. 'nickname' => $res['nickname'],
  51. 'mobile' => $res['mobile'],
  52. 'email' => $res['email'],
  53. 'token' => $token,
  54. 'expire_time' => $expire_time,
  55. 'supplier_list' => $list,
  56. ];
  57. //过期时间指定一个具体的日期的时候,还没到这个日期缓存就过期了,指定一个秒数,可以符合预期,邪门儿
  58. Cache::set(Config::get('redis_key.user_info_token') . $token, json_encode($info), $expire_int);
  59. return json_show(0, '登录成功', $info);
  60. }
  61. //获取供应商账号列表
  62. public static function getAccountList(array $param = [])
  63. {
  64. $db = new SupplierUserModel();
  65. $rs = $db
  66. ->where('is_del', $db::$is_del_normal);
  67. if ($param['keyword'] != '') $rs->whereLike('nickname|mobile', '%' . $param['keyword'] . '%');
  68. if ($param['status'] != '') $rs->where('status', $param['status']);
  69. if ($param['supplierNo'] != '') {
  70. $uids = SupplierRelationUserModel::where([
  71. 'is_del' => $db::$is_del_normal,
  72. 'supplierNo' => $param['supplierNo']
  73. ])->column('uid');
  74. $rs->whereIn('uid', $uids);
  75. }
  76. $count = $rs->count('uid');
  77. $list = $rs
  78. ->field('uid,nickname,mobile,email,status,addtime,creater')
  79. ->order('addtime', 'desc')
  80. ->page($param['page'], $param['size'])
  81. ->append(['supplier_list'])
  82. ->withAttr('supplier_list', function ($val, $data) use ($db) {
  83. return SupplierRelationUserModel::where([
  84. 'is_del' => $db::$is_del_normal,
  85. 'uid' => $data['uid']
  86. ])->field('supplierNo,supplierName,status')
  87. ->select()
  88. ->toArray();
  89. })
  90. ->select()
  91. ->toArray();
  92. return json_show(0, '获取成功', ['count' => $count, 'list' => $list]);
  93. }
  94. //修改供应商账号密码
  95. public static function changePassword(array $param = [])
  96. {
  97. $db = new SupplierUserModel();
  98. $res = $db
  99. ->where([
  100. 'uid' => $param['uid'],
  101. 'is_del' => $db::$is_del_normal,
  102. ])
  103. ->field('uid,status')
  104. ->findOrEmpty()
  105. ->toArray();
  106. if (empty($res)) return json_show(1004, '该账号不存在');
  107. if ($res['status'] == $db::$status_disabled) return json_show(1004, '该账号已禁用');
  108. //更新密码
  109. $password = get_encryption_password($param['password']);
  110. $rs = $db
  111. ->where('uid', $res['uid'])
  112. ->save(['password' => $password['password'], 'salt' => $password['salt']]);
  113. return $rs ? json_show(0, '修改密码成功') : json_show(1005, '修改密码失败');
  114. }
  115. //获取供应商账号账号信息
  116. public static function readAccount(array $param = [])
  117. {
  118. $res = SupplierUser::field('uid,nickname,mobile,email')
  119. ->where([
  120. 'uid' => $param['uid'],
  121. 'is_del' => SupplierUser::$is_del_normal,
  122. ])
  123. ->append(['supplier_list'])
  124. ->withAttr('supplier_list', function ($val, $data) {
  125. return SupplierRelationUserModel::field('id,supplierNo,supplierName,status,addtime')
  126. ->where([
  127. 'is_del' => SupplierUser::$is_del_normal,
  128. 'uid' => $data['uid']
  129. ])
  130. ->select()
  131. ->toArray();
  132. })
  133. ->findOrEmpty()
  134. ->toArray();
  135. return json_show(0, '获取账号信息成功', $res);
  136. }
  137. //添加供应商账号
  138. public static function addAccount(array $param = [], string $token = '')
  139. {
  140. $user = GetUserInfo($token);
  141. //查找供应商名称
  142. $supplierName = Db::name('supplier')
  143. ->whereIn('code', $param['supplierNo'])
  144. ->column('name', 'code');
  145. if (empty($supplierName)) return json_show(1004, '供应商不存在');
  146. Db::connect('mysql_sys')->startTrans();
  147. try {
  148. $db = new SupplierUserModel();
  149. $res = $db
  150. ->field('uid')
  151. ->where([
  152. 'is_del' => $db::$is_del_normal,
  153. 'mobile' => $param['mobile'],
  154. ])->findOrEmpty();
  155. if (!$res->isEmpty()) throw new Exception('该手机号已存在');
  156. //新增账号
  157. $password = get_encryption_password(Config::get('app.default_password'));
  158. $uid = $db->insertGetId([
  159. 'nickname' => $param['nickname'],//姓名
  160. 'mobile' => $param['mobile'],//手机号
  161. 'email' => $param['email'],//邮箱
  162. 'password' => $password['password'],//密码密文
  163. 'salt' => $password['salt'],//盐值
  164. 'status' => $db::$status_normal,
  165. 'is_del' => $db::$is_del_normal,
  166. 'createrid' => $user['data']['user_id'] ?? 0,
  167. 'creater' => $user['data']['nickname'] ?? '',
  168. ]);
  169. $insert_data = [];
  170. foreach ($param['supplierNo'] as $supplierNo) {
  171. $insert_data[] = [
  172. 'uid' => $uid,
  173. 'supplierNo' => $supplierNo,
  174. 'supplierName' => $supplierName[$supplierNo] ?? '',
  175. 'status' => $db::$status_normal,
  176. 'is_del' => $db::$is_del_normal,
  177. 'createrid' => $user['data']['user_id'] ?? 0,
  178. 'creater' => $user['data']['nickname'] ?? '',
  179. ];
  180. }
  181. if ($insert_data) Db::connect('mysql_sys')
  182. ->name('supplier_relation_user')
  183. ->insertAll($insert_data);
  184. Db::connect('mysql_sys')->commit();
  185. return json_show(0, '操作成功');
  186. } catch (Exception $exception) {
  187. Db::connect('mysql_sys')->rollback();
  188. return json_show(1005, '操作失败' . $exception->getMessage());
  189. }
  190. }
  191. //修改供应商账号
  192. public static function editAccount(array $param = [])
  193. {
  194. $user = GetUserInfo($param['token']);
  195. Db::connect('mysql_sys')->startTrans();
  196. try {
  197. $db = new SupplierUserModel();
  198. $res = $db
  199. ->field('uid,mobile')
  200. ->where(['uid' => $param['uid'], 'is_del' => $db::$is_del_normal])
  201. ->findOrEmpty()
  202. ->toArray();
  203. if (empty($res)) return json_show(1004, '该账号不存在');
  204. if ($res['mobile'] != $param['mobile']) {
  205. $temp = $db
  206. ->field('uid')
  207. ->where(['mobile' => $param['mobile'], 'is_del' => $db::$is_del_normal])
  208. ->where('uid', '<>', $param['uid'])
  209. ->findOrEmpty()
  210. ->isEmpty();
  211. if (!$temp) throw new Exception('要修改的手机号已存在');
  212. }
  213. $db
  214. ->where('uid', $param['uid'])
  215. ->strict(false)
  216. ->save($param);
  217. $relation_db = new SupplierRelationUserModel();
  218. $insert = $retain = [];
  219. foreach ($param['supplier_list'] as $supplier) {
  220. if (isset($supplier['id']) && $supplier['id'] != 0) $retain[] = $supplier['id'];
  221. else $insert[] = [
  222. 'uid' => $param['uid'],
  223. 'supplierNo' => $supplier['supplierNo'],
  224. 'supplierName' => $supplier['supplierName'],
  225. 'status' => $db::$status_normal,
  226. 'is_del' => $db::$is_del_normal,
  227. 'createrid' => $user['data']['user_id'] ?? 0,
  228. 'creater' => $user['data']['nickname'] ?? 0,
  229. ];
  230. }
  231. //除了保留id,其余删除
  232. if (!empty($retain)) $relation_db->where([
  233. 'is_del' => $db::$is_del_normal,
  234. 'uid' => $param['uid']
  235. ])->whereNotIn('id', $retain)->save(['is_del' => $db::$is_del_deleted]);
  236. if ($insert) $relation_db->saveAll($insert);
  237. Db::connect('mysql_sys')->commit();
  238. return json_show(0, '操作成功');
  239. } catch (Exception $exception) {
  240. Db::connect('mysql_sys')->rollback();
  241. return json_show(1005, '操作失败,' . $exception->getMessage());
  242. }
  243. }
  244. //修改供应商账号状态
  245. public static function statusAccount(array $param = [])
  246. {
  247. Db::connect('mysql_sys')->startTrans();
  248. try {
  249. $db = new SupplierUserModel();
  250. $res = $db
  251. ->field('uid,status')
  252. ->where([
  253. 'uid' => $param['uid'],
  254. 'is_del' => $db::$is_del_normal,
  255. ])->findOrEmpty();
  256. if ($res->isEmpty()) throw new Exception('该账号不存在');
  257. if ($res->status == $param['status']) throw new Exception('不能重复操作');
  258. $db->where([
  259. 'uid' => $param['uid'],
  260. 'is_del' => $db::$is_del_normal,
  261. ])->where('status', '<>', $param['status'])
  262. ->save(['status' => $param['status']]);
  263. SupplierRelationUserModel::where([
  264. 'uid' => $param['uid'],
  265. 'is_del' => $db::$is_del_normal,
  266. ])->where('status', '<>', $param['status'])
  267. ->save(['status' => $param['status']]);
  268. Db::connect('mysql_sys')->commit();
  269. return json_show(0, '操作成功');
  270. } catch (Exception $exception) {
  271. Db::connect('mysql_sys')->rollback();
  272. return json_show(1005, '操作失败,' . $exception->getMessage());
  273. }
  274. }
  275. //删除供应商账号
  276. public static function deleteAccount(int $uid = 0)
  277. {
  278. Db::connect('mysql_sys')->startTrans();
  279. try {
  280. $db = new SupplierUserModel();
  281. $res = $db
  282. ->field('uid')
  283. ->where([
  284. 'uid' => $uid,
  285. 'is_del' => $db::$is_del_normal,
  286. ])->findOrEmpty();
  287. if ($res->isEmpty()) throw new Exception('该账号不存在或已删除');
  288. $db->where('uid', $uid)
  289. ->where('is_del', $db::$is_del_normal)
  290. ->save(['is_del' => $db::$is_del_deleted]);
  291. SupplierRelationUserModel::where('uid', $uid)
  292. ->where('is_del', $db::$is_del_normal)
  293. ->save(['is_del' => $db::$is_del_deleted]);
  294. Db::connect('mysql_sys')->commit();
  295. return json_show(0, '删除成功');
  296. } catch (Exception $exception) {
  297. Db::connect('mysql_sys')->rollback();
  298. return json_show(1005, '删除失败,' . $exception->getMessage());
  299. }
  300. }
  301. }