Companys.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Exception;
  6. class Companys extends Api
  7. {
  8. protected $noNeedLogin = ['getList','getInfo'];
  9. protected $noNeedRight = '*';
  10. protected $model = null;
  11. public function _initialize()
  12. {
  13. parent::_initialize();
  14. $this->model = Db::name('company');
  15. }
  16. /**
  17. * 列表
  18. * @return void
  19. */
  20. public function getList()
  21. {
  22. try {
  23. $page = $this->request->param('page',1);
  24. $limit = $this->request->param('listrow',15);
  25. $offset = ($page - 1) * $limit;
  26. //获取门店信息
  27. $params = [
  28. 'lng' => $this->request->param('lng',''),
  29. 'lat' => $this->request->param('lat',''),
  30. ];
  31. $companyList = $this->getCompanySort($params);
  32. $result = array_slice($companyList['data'],$offset,$limit);
  33. $this->success('获取成功',$result);
  34. } catch (Exception $e) {
  35. $this->error($e->getMessage());
  36. }
  37. }
  38. /**
  39. * 获取门店距离排序
  40. * @return void
  41. */
  42. public function getCompanySort($params=[])
  43. {
  44. try {
  45. $result = [
  46. 'status' => true,
  47. 'msg' => '获取成功',
  48. 'data' => [],
  49. ];
  50. $lng = isset($params['lng']) ? $params['lng'] : '';
  51. $lat = isset($params['lat']) ? $params['lat'] : '';
  52. $field = 'id,name,image,contacts,mobile,full_address,longitude,latitude,is_open,open_hours';
  53. $where['status'] = 1;
  54. $data = $this->model->where($where)->field($field)->select();
  55. if (!empty($data)) {
  56. foreach ($data as $key => &$value) {
  57. $value['image'] = !empty($value['image']) ? cdnurl($value['image']) : '';
  58. $value['distance'] = getDistance($lng, $lat, $value['longitude'], $value['latitude'], true, 2);
  59. $value['distance_text'] = !empty($value['distance']) ? $value['distance'].'km' : '';
  60. }
  61. $n = count($data);
  62. for($i=1;$i<$n;$i++) { //从第二个元素开始插入
  63. for($j=$i-1;$j>=0;$j--) { //与前面的数比较,找到插入的位置
  64. if($data[$j]['distance'] > $data[$j+1]['distance']) { //比前面的数小,交换位置
  65. $tmp = $data[$j];
  66. $data[$j] = $data[$j+1];
  67. $data[$j+1] = $tmp;
  68. } else { //大于或等于前面的数,表示已找到插入的位置
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. $result['data'] = $data;
  75. return $result;
  76. } catch (Exception $e) {
  77. $result['status'] = 0;
  78. $result['msg'] = $e->getMessage();
  79. return $result;
  80. }
  81. }
  82. /**
  83. * 详情
  84. * @return void
  85. */
  86. public function getInfo()
  87. {
  88. try {
  89. $id = $this->request->param('id',0);
  90. $field = 'id,name,image,contacts,mobile,full_address,longitude,latitude,is_open,open_hours';
  91. $where['status'] = 1;
  92. $where['id'] = $id;
  93. $result = $this->model->where($where)->field($field)->find();
  94. if (!empty($result)) {
  95. !empty($result['image']) && $result['image'] = cdnurl($result['image']);
  96. }
  97. $this->success('获取成功',$result);
  98. } catch (Exception $e) {
  99. $this->error($e->getMessage());
  100. }
  101. }
  102. /**
  103. * 绑定门店
  104. * @return void
  105. */
  106. public function bind()
  107. {
  108. Db::startTrans();
  109. try {
  110. $id = $this->request->param('id',0);
  111. $comeform = $this->request->param('comeform','');
  112. $where['id'] = $id;
  113. $where['status'] = 1;
  114. $time = time();
  115. $company = $this->model->where($where)->find();
  116. if (empty($company)) {
  117. throw new Exception('未找到门店信息');
  118. }
  119. $user = $this->auth->getUser();
  120. $userId = $this->auth->id;
  121. if ($user['company_id'] != $id) {//更新绑定门店
  122. $userWhere['id'] = $userId;
  123. $userData['company_id'] = $id;
  124. $userRes = model('User')->where($userWhere)->update($userData);
  125. if (!$userRes) {
  126. throw new Exception('绑定门店失败');
  127. }
  128. }
  129. //用户门店钱包
  130. $walletWhere['user_id'] = $userId;
  131. $walletWhere['company_id'] = $id;
  132. $userWallet = model('UserWallet')->where($walletWhere)->find();
  133. if (empty($userWallet)) {
  134. $walletData = [
  135. 'user_id' => $userId,
  136. 'company_id' => $id,
  137. 'staff_id' => 0,
  138. 'money' => 0.00,
  139. 'address' => '',
  140. 'createtime' => $time,
  141. 'updatetime' => $time,
  142. 'remark' => '',
  143. 'comefrom' => $comeform,
  144. ];
  145. $walletRes = model('UserWallet')->insertGetId($walletData);
  146. if (!$walletRes) {
  147. throw new Exception('绑定失败');
  148. }
  149. }
  150. Db::commit();
  151. $this->success();
  152. } catch (Exception $e) {
  153. Db::rollback();
  154. $this->error($e->getMessage());
  155. }
  156. }
  157. public function bindTemp()
  158. {
  159. try {
  160. $id = $this->request->param('id',0);
  161. $where['id'] = $id;
  162. $where['status'] = 1;
  163. $company = $this->model->where($where)->find();
  164. if (empty($company)) {
  165. throw new Exception('未找到门店信息');
  166. }
  167. $userId = $this->auth->id;
  168. $userWhere['id'] = $userId;
  169. $user = Db::name('user')->where($userWhere)->find();
  170. if ($user['temp_company_id'] != $id) {//更新绑定门店
  171. $userData['temp_company_id'] = $id;
  172. $userRes = model('User')->where($userWhere)->update($userData);
  173. if (!$userRes) {
  174. throw new Exception('绑定门店失败');
  175. }
  176. }
  177. $this->success();
  178. } catch (Exception $e) {
  179. $this->error($e->getMessage());
  180. }
  181. }
  182. /**
  183. * 保存
  184. * @return void
  185. */
  186. public function save()
  187. {
  188. try {
  189. //验证参数
  190. $id = $this->request->param('id',0);
  191. $userId = $this->auth->id;
  192. if (empty($id)) {
  193. $companyWhere['user_id'] = $userId;
  194. $company = $this->model->where($companyWhere)->find();
  195. if (!empty($company)) {
  196. $id = $company['id'];
  197. }
  198. }
  199. $scene = !empty($id) ? 'edit' : 'add';
  200. $validate = validate('Companys');
  201. if(!$validate->check($this->request->param(),[],$scene)){
  202. throw new Exception($validate->getError());
  203. }
  204. if (!empty($id)) {
  205. $where['user_id'] = $userId;
  206. $where['id'] = $id;
  207. $companyData = $this->model->where($where)->find();
  208. if (empty($companyData)) {
  209. throw new Exception('未找到相关信息');
  210. }
  211. } else {
  212. $where['user_id'] = $userId;
  213. $where['status'] = 1;//状态:-1=新来的,0=待审核,1=审核通过,2=审核不通过
  214. $companyData = $this->model->where($where)->find();
  215. if (!empty($companyData)) {
  216. throw new Exception('您已申请过入驻');
  217. }
  218. }
  219. $time = time();
  220. $areaData = getProvince($this->request->param());
  221. $fullAddress = $areaData['full_address'];
  222. $data = [
  223. 'contacts' => $this->request->param('contacts', ''),
  224. 'mobile' => $this->request->param('mobile', ''),
  225. 'province_id' => $this->request->param('province_id', 0),
  226. 'city_id' => $this->request->param('city_id', 0),
  227. 'area_id' => $this->request->param('area_id', 0),
  228. 'province_name' => $areaData['province_name'],
  229. 'city_name' => $areaData['city_name'],
  230. 'area_name' => $areaData['area_name'],
  231. 'address' => $this->request->param('address', ''),
  232. 'full_address' => $fullAddress,
  233. 'aptitude_images' => $this->request->param('aptitude_images', ''),
  234. 'status' => -1,
  235. ];
  236. if (empty($id)) {
  237. $data['image'] = '/assets/img/avatar.png';//默认头像
  238. $data['user_id'] = $userId;
  239. $data['createtime'] = $time;
  240. $res = $this->model->insertGetId($data);
  241. if ($res) {//生成门店钱包
  242. $companyAddData = [
  243. 'user_id' => $res,
  244. 'money' => 0.00,
  245. ];
  246. $companyRes = Db::name('company_wallet')->insertGetId($companyAddData);
  247. if (!$companyRes) {
  248. throw new Exception('入驻提交失败');
  249. }
  250. }
  251. } else {
  252. $data['updatetime'] = $time;
  253. $where['id'] = $id;
  254. $where['user_id'] = $userId;
  255. $res = $this->model->where($where)->update($data);
  256. }
  257. if (!$res) {
  258. throw new Exception('操作失败');
  259. }
  260. $this->success('操作成功');
  261. } catch (Exception $e) {
  262. $this->error($e->getMessage());
  263. }
  264. }
  265. /**
  266. * 入驻详情
  267. * @return void
  268. */
  269. public function getCompanyInfo()
  270. {
  271. try {
  272. $userId = $this->auth->id;
  273. $field = 'id,contacts,mobile,province_id,city_id,area_id,province_name,city_name,area_name,address,aptitude_images,status';
  274. $where['user_id'] = $userId;
  275. $result = $this->model->where($where)->field($field)->find();
  276. if (!empty($result)) {
  277. $statusArr = model('Company')->getStatusList();
  278. $result['status_text'] = isset($statusArr[$result['status']]) ? $statusArr[$result['status']] : '';
  279. $result = info_domain_image($result,['aptitude_images']);
  280. }
  281. $this->success('获取成功',$result);
  282. } catch (Exception $e) {
  283. $this->error($e->getMessage());
  284. }
  285. }
  286. }