Account.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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([
  38. 'is_del' => $db::$is_del_normal,
  39. 'uid' => $res['uid'],
  40. ])
  41. ->select()
  42. ->toArray();
  43. $info = [
  44. 'uid' => $res['uid'],
  45. 'nickname' => $res['nickname'],
  46. 'mobile' => $res['mobile'],
  47. 'email' => $res['email'],
  48. 'token' => $token,
  49. 'expire_time' => $expire_time,
  50. 'supplier_list' => $list,
  51. ];
  52. //过期时间指定一个具体的日期的时候,还没到这个日期缓存就过期了,指定一个秒数,可以符合预期,邪门儿
  53. Cache::set(Config::get('redis_key.user_info_token') . $token, json_encode($info), $expire_int);
  54. return json_show(0, '登录成功', $info);
  55. }
  56. //获取供应商账号列表
  57. public static function getAccountList(array $param = [])
  58. {
  59. $db = new SupplierRelationUserModel();
  60. $rs = $db
  61. ->alias('a')
  62. ->leftJoin('supplier_user b', 'b.uid=a.uid AND b.is_del=0')
  63. ->where([
  64. 'a.is_del' => $db::$is_del_normal,
  65. 'a.supplierNo' => $param['supplierNo'],
  66. ]);
  67. if ($param['keyword'] != '') $rs->whereLike('b.nickname|b.mobile', '%' . $param['keyword'] . '%');
  68. if ($param['status'] != '') $rs->where('a.status', $param['status']);
  69. $count = $rs->count('a.id');
  70. $list = $rs
  71. ->field('a.id,b.uid,b.nickname,b.mobile,b.email,a.status,a.addtime,a.creater')
  72. ->order('a.addtime', 'desc')
  73. ->page($param['page'], $param['size'])
  74. ->select()
  75. ->toArray();
  76. return json_show(0, '获取成功', ['count' => $count, 'list' => $list]);
  77. }
  78. //修改供应商账号密码
  79. public static function changePassword(array $param = [])
  80. {
  81. $db = new SupplierUserModel();
  82. $res = $db
  83. ->where([
  84. 'uid' => $param['uid'],
  85. 'is_del' => $db::$is_del_normal,
  86. ])
  87. ->field('uid,password,salt,status,is_del')
  88. ->findOrEmpty()
  89. ->toArray();
  90. if (empty($res)) return json_show(1004, '该账号不存在');
  91. if ($res['status'] == $db::$status_disabled) return json_show(1004, '该账号已禁用');
  92. $old_password = get_encryption_password($param['old_password'], $res['salt']);
  93. if ($old_password['password'] != $res['password']) return json_show(1004, '密码错误');
  94. //更新密码
  95. $password = get_encryption_password($param['new_password']);
  96. $rs = $db
  97. ->where('uid', $res['uid'])
  98. ->save(['password' => $password['password'], 'salt' => $password['salt']]);
  99. return $rs ? json_show(0, '修改密码成功') : json_show(1005, '修改密码失败');
  100. }
  101. //获取供应商账号账号信息
  102. public static function readAccount(array $param = [])
  103. {
  104. $res = SupplierUser::field('uid,nickname,mobile,email')
  105. ->where([
  106. 'uid' => $param['uid'],
  107. 'is_del' => SupplierUser::$is_del_normal,
  108. ])
  109. ->append(['supplier_list'])
  110. ->withAttr('supplier_list', function ($val, $data) {
  111. return SupplierRelationUserModel::field('id,supplierNo,supplierName,status,addtime')
  112. ->where([
  113. 'is_del' => SupplierUser::$is_del_normal,
  114. 'uid' => $data['uid']
  115. ])
  116. ->select()
  117. ->toArray();
  118. })
  119. ->findOrEmpty()
  120. ->toArray();
  121. return json_show(0, '获取账号信息成功', $res);
  122. }
  123. //添加供应商账号
  124. public static function addAccount(array $param = [], string $token = '')
  125. {
  126. $user = GetUserInfo($token);
  127. //查找供应商名称
  128. $supplierName = Db::name('supplier')
  129. ->where(['code' => $param['supplierNo']])
  130. ->value('name', '');
  131. if ($supplierName == '') return json_show(1004, '该供应商名称不存在');
  132. Db::connect('mysql_sys')->startTrans();
  133. try {
  134. $db = new SupplierUserModel();
  135. $res = $db
  136. ->field('uid,status')
  137. ->where([
  138. 'is_del' => $db::$is_del_normal,
  139. 'mobile' => $param['mobile'],
  140. ])->findOrEmpty();
  141. $is_insert = true;
  142. if ($res->isEmpty()) {
  143. //新增账号
  144. $password = get_encryption_password(Config::get('app.default_password'));
  145. $res->uid = $db->insertGetId([
  146. 'nickname' => $param['nickname'],//姓名
  147. 'mobile' => $param['mobile'],//手机号
  148. 'email' => $param['email'],//邮箱
  149. 'password' => $password['password'],//密码密文
  150. 'salt' => $password['salt'],//盐值
  151. 'status' => $db::$status_normal,
  152. 'is_del' => $db::$is_del_normal,
  153. 'createrid' => $user['data']['user_id'] ?? 0,
  154. 'creater' => $user['data']['nickname'] ?? '',
  155. ]);
  156. $res->status = $db::$status_normal;
  157. } else {
  158. $rs = SupplierRelationUserModel::field('id')
  159. ->where([
  160. 'is_del' => $db::$is_del_normal,
  161. 'uid' => $res->uid,
  162. 'supplierNo' => $param['supplierNo'],
  163. ])->findOrEmpty()
  164. ->isEmpty();
  165. if ($rs) throw new Exception('不能重复添加');//该关联关系已存在,无需新增
  166. else $is_insert = false;
  167. }
  168. if ($is_insert) {
  169. SupplierRelationUserModel::create([
  170. 'uid' => $res->uid,
  171. 'supplierNo' => $param['supplierNo'],
  172. 'supplierName' => $supplierName,
  173. 'status' => $res->status,
  174. 'is_del' => $db::$is_del_normal,
  175. 'createrid' => $user['data']['user_id'] ?? 0,
  176. 'creater' => $user['data']['nickname'] ?? '',
  177. ])->save();
  178. }
  179. Db::connect('mysql_sys')->commit();
  180. return json_show(0, '操作成功');
  181. } catch (Exception $exception) {
  182. Db::connect('mysql_sys')->rollback();
  183. return json_show(1005, '操作失败' . $exception->getMessage());
  184. }
  185. }
  186. //修改供应商账号
  187. public static function editAccount(array $param = [])
  188. {
  189. $db = new SupplierUserModel();
  190. $res = $db
  191. ->field('uid')
  192. ->where(['uid' => $param['uid'], 'is_del' => $db::$is_del_normal])
  193. ->findOrEmpty()
  194. ->isEmpty();
  195. if ($res) return json_show(1004, '该账号不存在');
  196. $rs = $db
  197. ->where('uid', $param['uid'])
  198. ->save($param);
  199. return $rs ? json_show(0, '修改成功') : json_show(1005, '修改失败');
  200. }
  201. //修改供应商账号状态
  202. public static function statusAccount(array $param = [])
  203. {
  204. Db::connect('mysql_sys')->startTrans();
  205. try {
  206. $db = new SupplierUserModel();
  207. $res = $db
  208. ->field('uid,status')
  209. ->where([
  210. 'uid' => $param['uid'],
  211. 'is_del' => $db::$is_del_normal,
  212. ])->findOrEmpty();
  213. if ($res->isEmpty()) throw new Exception('该账号不存在');
  214. if ($res->status == $param['status']) throw new Exception('不能重复操作');
  215. $db->where([
  216. 'uid' => $param['uid'],
  217. 'is_del' => $db::$is_del_normal,
  218. ])->where('status', '<>', $param['status'])
  219. ->save(['status' => $param['status']]);
  220. SupplierRelationUserModel::where([
  221. 'uid' => $param['uid'],
  222. 'is_del' => $db::$is_del_normal,
  223. ])->where('status', '<>', $param['status'])
  224. ->save(['status' => $param['status']]);
  225. Db::connect('mysql_sys')->commit();
  226. return json_show(0, '操作成功');
  227. } catch (Exception $exception) {
  228. Db::connect('mysql_sys')->rollback();
  229. return json_show(1005, '操作失败,' . $exception->getMessage());
  230. }
  231. }
  232. //删除供应商账号
  233. public static function deleteAccount(int $uid = 0)
  234. {
  235. Db::connect('mysql_sys')->startTrans();
  236. try {
  237. $db = new SupplierUserModel();
  238. $res = $db
  239. ->field('uid')
  240. ->where([
  241. 'uid' => $uid,
  242. 'is_del' => $db::$is_del_normal,
  243. ])->findOrEmpty();
  244. if ($res->isEmpty()) throw new Exception('该账号不存在或已删除');
  245. $db->where('uid', $uid)
  246. ->where('is_del', $db::$is_del_normal)
  247. ->save(['is_del' => $db::$is_del_deleted]);
  248. SupplierRelationUserModel::where('uid', $uid)
  249. ->where('is_del', $db::$is_del_normal)
  250. ->save(['is_del' => $db::$is_del_deleted]);
  251. Db::connect('mysql_sys')->commit();
  252. return json_show(0, '删除成功');
  253. } catch (Exception $exception) {
  254. Db::connect('mysql_sys')->rollback();
  255. return json_show(1005, '删除失败,' . $exception->getMessage());
  256. }
  257. }
  258. }