Attire.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Exception;
  6. /**
  7. * 装扮商城接口
  8. */
  9. class Attire extends Api
  10. {
  11. protected $noNeedLogin = ['getAttireList'];
  12. protected $noNeedRight = '*';
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. $this->attireModel = new \app\common\model\Attire();
  17. $this->attirebackModel = new \app\common\model\AttireBack();
  18. }
  19. /**
  20. * 获取装扮列表
  21. */
  22. public function getAttireList() {
  23. $type = $this->request->request("type"); // 装扮类型:1=座驾,2=头饰,3=聊天气泡,4=消息尾灯
  24. $page = $this->request->request('page',1); // 分页
  25. $pageNum = $this->request->request('pageNum',10); // 分页
  26. // 分页搜索构建
  27. $pageStart = ($page-1)*$pageNum;
  28. // 获取基本信息
  29. $where = [];
  30. $where["type"] = $type;
  31. $attireList = $this->attireModel->where($where)->limit($pageStart,$pageNum)->select();
  32. if($attireList) {
  33. foreach($attireList as $k => $v) {
  34. }
  35. }
  36. $this->success("获取成功!",$attireList);
  37. }
  38. /**
  39. * 购买并加入我的背包
  40. */
  41. public function attireAddToMyBack() {
  42. $attire_id = $this->request->request("attire_id"); //装扮ID
  43. if (!$attire_id) {
  44. $this->error(__('Invalid parameters'));
  45. }
  46. if ($this->auth->power->attire == 1) {
  47. $this->error('您已被禁止购买装扮');
  48. }
  49. // 判断用户钻石余额是否充足
  50. $userModel = new \app\common\model\User();
  51. $userInfo = $userModel->where(["id"=>$this->auth->id])->find();
  52. if(!$userInfo) {
  53. $this->error("用户信息获取失败!");
  54. }
  55. // 获取购买装扮需要的价格
  56. $attireInfo = $this->attireModel->where(["id"=>$attire_id])->find();
  57. if(!$attireInfo) {
  58. $this->error("装扮信息获取失败!");
  59. }
  60. if($userInfo["jewel"] < $attireInfo["price"]) {
  61. $this->error("您的余额不足,请先充值!");
  62. }
  63. // 进行购买逻辑
  64. Db::startTrans();
  65. try{
  66. $userjewellogModel = new \app\common\model\UserJewelLog();
  67. // 扣除用户钻石余额
  68. $where = [];
  69. $where["id"] = $this->auth->id;
  70. $res1 = $userModel->where($where)->setDec("jewel",$attireInfo["price"]);
  71. // 添加当前用户钻石流水记录
  72. $res2 = $userjewellogModel->addUserJewelLog($this->auth->id, $attireInfo["price"], "-", $userInfo["jewel"], "购买装扮扣除" . $attireInfo["price"] . "钻石!", 6);
  73. // 添加装扮购买记录
  74. $data = [];
  75. $data["user_id"] = $this->auth->id;
  76. $data["attire_id"] = $attire_id;
  77. $data["price"] = $attireInfo["price"];
  78. $data["attire_name"] = $attireInfo["title"];
  79. $data["createtime"] = time();
  80. $attirebylogModel = new \app\common\model\AttireBuyLog();
  81. $res3 = $attirebylogModel->insertGetId($data);
  82. // 加入我的背包
  83. // $where = [];
  84. // $where["user_id"] = $this->auth->id;
  85. // $where["attire_id"] = $attire_id;
  86. // $attirebackInfo = $this->attirebackModel->where($where)->find();
  87. $data = [];
  88. $data["user_id"] = $this->auth->id;
  89. $data["attire_id"] = $attire_id;
  90. $data["attire_name"] = $attireInfo["title"];
  91. $data["price"] = $attireInfo["price"];
  92. $data["file_image"] = $attireInfo["file_image"];
  93. $data["android_image"] = $attireInfo["android_image"];
  94. $data["gif_image"] = $attireInfo["gif_image"];
  95. $data["limit_day"] = $attireInfo["limit_day"];
  96. $data["type"] = $attireInfo["type"];
  97. $data["createtime"] = time();
  98. // if($attirebackInfo) { // 背包中已有商品
  99. // if($attirebackInfo["is_use"] == 1) { // 商品正在使用中
  100. // if($attirebackInfo["duetime"] >= time()) { // 使用中未到期
  101. // $duetime = $attireInfo["limit_day"]*86400;
  102. // $res4 = $this->attirebackModel->setInc("duetime",$duetime);
  103. // } else { // 使用中已到期
  104. // $res4 = $this->attirebackModel->insert($data);
  105. // }
  106. // } else { // 商品未使用
  107. // $res4 = $this->attirebackModel->insert($data);
  108. // }
  109. // } else { // 背包中没有此商品
  110. // $res4 = $this->attirebackModel->insert($data);
  111. // }
  112. $res4 = $this->attirebackModel->insert($data);
  113. if($res1 && $res2 && $res3 && $res4) {
  114. Db::commit();
  115. $this->success("购买成功!");
  116. } else {
  117. $this->error("购买失败,请稍后重试!");
  118. }
  119. }catch (ValidateException $e) {
  120. Db::rollback();
  121. $this->error($e->getMessage());
  122. } catch (PDOException $e) {
  123. Db::rollback();
  124. $this->error($e->getMessage());
  125. } catch (Exception $e) {
  126. Db::rollback();
  127. $this->error($e->getMessage());
  128. }
  129. }
  130. /**
  131. * 获取我的背包装饰列表
  132. */
  133. public function getBackAttireList() {
  134. try {
  135. $page = $this->request->request('page',1); // 分页
  136. $pageNum = $this->request->request('pageNum',10); // 分页
  137. $type = $this->request->request("type"); // 装扮类型:1=座驾,2=头饰,3=聊天气泡,4=消息尾灯
  138. // 分页搜索构建
  139. $pageStart = ($page-1)*$pageNum;
  140. global $where;
  141. $where = [];$whereOr=[];
  142. $where["user_id"] = $this->auth->id;
  143. $type && $where["type"] = $type;
  144. $whereOr["is_use"] = "0";
  145. $whereOr["duetime"] = ["gt",time()];
  146. $list = $this->attirebackModel
  147. ->field("id,type,file_image,gif_image,attire_name as title,limit_day,duetime,is_use,is_using")
  148. ->where(function ($query) {
  149. global $where;
  150. $query->where($where);
  151. })
  152. ->where(function ($query) {
  153. $query->whereOr(["is_use"=>0,"duetime"=>["gt",time()]]);
  154. })
  155. ->limit($pageStart,$pageNum)
  156. ->select();
  157. if(!$list) {
  158. $this->success("背包空空如也!");
  159. }
  160. foreach($list as $k => $v) {
  161. // 有效期/剩余天数
  162. if($v["is_use"] == 1 && $v["duetime"]>time()) { // 正在使用,未过期
  163. $dtime = "剩余".ceil(($v["duetime"]-time())/86400)."天";
  164. }
  165. if($v["is_use"] == 0) {
  166. $dtime = "有效期".$v["limit_day"]."天";
  167. }
  168. $list[$k]["dtime"] = $dtime;
  169. }
  170. $this->success("获取成功!",$list);
  171. } catch (Exception $e) {echo '<pre>';var_dump($e->getMessage());exit;
  172. $this->error($e->getMessage());
  173. }
  174. }
  175. /**
  176. * 使用并装上装饰
  177. */
  178. public function toUseAttire() {
  179. $id = $this->request->request("id"); //背包中商品ID
  180. if (!$id) {
  181. $this->error(__('Invalid parameters'));
  182. }
  183. // 获取装饰信息
  184. $where = [];
  185. $where["id"] = $id;
  186. $attirebackInfo = $this->attirebackModel->where($where)->find();
  187. $data = [];
  188. $data["is_use"] = 1;
  189. $data["is_using"] = 1;
  190. $data["duetime"] = time()+$attirebackInfo["limit_day"]*86400;
  191. $res = $this->attirebackModel->update($data,$where);
  192. if($res) {
  193. $this->success("设置成功!");
  194. } else {
  195. $this->success("网络错误,请稍后重试!");
  196. }
  197. }
  198. /**
  199. * 装上
  200. */
  201. public function toUsingAttire() {
  202. $id = $this->request->request("id"); //背包中商品ID
  203. $use_type = $this->request->request("use_type",1); //使用方式:1=装上,2=卸下
  204. if (!$id || !in_array($use_type,[1,2])) {
  205. $this->error(__('Invalid parameters'));
  206. }
  207. // 获取装饰信息
  208. $where = [];
  209. $where["id"] = $id;
  210. $attirebackInfo = $this->attirebackModel->where($where)->find();
  211. if($use_type == 2) {
  212. $attirebackInfo->is_using = 0;
  213. $attirebackInfo->save();
  214. $this->success("设置成功!");
  215. }
  216. if($attirebackInfo["is_use"] != 1) {
  217. $data = [];
  218. $data["is_use"] = 1;
  219. $data["is_using"] = 1;
  220. $data["duetime"] = time()+$attirebackInfo["limit_day"]*86400;
  221. $res = $this->attirebackModel->update($data,$where);
  222. if(!$res) $this->error("使用失败!");
  223. }
  224. Db::startTrans();
  225. try{
  226. // 先取消掉所有的
  227. $res1 = $this->attirebackModel->update(["is_using"=>0],["user_id"=>$this->auth->id,"type"=>$attirebackInfo["type"]]);
  228. // 再设置当前
  229. $res2 = $this->attirebackModel->update(["is_using"=>1],["id"=>$id]);
  230. if($res1 && $res2) {
  231. Db::commit();
  232. $this->success("设置成功!");
  233. } else {
  234. $this->success("网络错误,请稍后重试!");
  235. }
  236. }catch (ValidateException $e) {
  237. Db::rollback();
  238. $this->error($e->getMessage());
  239. } catch (PDOException $e) {
  240. Db::rollback();
  241. $this->error($e->getMessage());
  242. } catch (Exception $e) {
  243. Db::rollback();
  244. $this->error($e->getMessage());
  245. }
  246. }
  247. }