Sellervoucher.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. use think\facade\Db;
  6. /**
  7. * ============================================================================
  8. *
  9. * ============================================================================
  10. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  11. * 网站地址: https://www.valimart.net/
  12. * ----------------------------------------------------------------------------
  13. *
  14. * ============================================================================
  15. * 卖家代金券控制器
  16. */
  17. class Sellervoucher extends MobileSeller {
  18. private $quotastate_arr;
  19. private $templatestate_arr;
  20. public function initialize() {
  21. parent::initialize(); // TODO: Change the autogenerated stub
  22. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellervoucher.lang.php');
  23. if (config('ds_config.voucher_allow') != 1) {
  24. ds_json_encode(10001, lang('voucher_unavailable'));
  25. }
  26. //套餐状态
  27. $this->quotastate_arr = array(
  28. 'activity' => array(1, lang('voucher_quotastate_activity')),
  29. 'cancel' => array(2, lang('voucher_quotastate_cancel')),
  30. 'expire' => array(3, lang('voucher_quotastate_expire'))
  31. );
  32. //代金券模板状态
  33. $this->templatestate_arr = array(
  34. 'usable' => array(1, lang('voucher_templatestate_usable')),
  35. 'disabled' => array(2, lang('voucher_templatestate_disabled'))
  36. );
  37. }
  38. /**
  39. * @api {POST} api/Sellervoucher/templatelist 代金券列表
  40. * @apiVersion 1.0.0
  41. * @apiGroup Sellervoucher
  42. *
  43. * @apiHeader {String} X-DS-KEY 卖家授权token
  44. *
  45. * @apiParam {String} txt_keyword 关键词
  46. * @apiParam {String} select_state 状态 1:有效 2:失效
  47. * @apiParam {String} txt_startdate 有效期开始时间 YYYY-MM-DD
  48. * @apiParam {String} txt_enddate 有效期结束时间 YYYY-MM-DD
  49. * @apiParam {String} page 页码
  50. * @apiParam {String} pagesize 每页显示数量
  51. *
  52. * @apiSuccess {String} code 返回码,10000为成功
  53. * @apiSuccess {String} message 返回消息
  54. * @apiSuccess {Object} result 返回数据
  55. * @apiSuccess {Object} result.current_quota 代金券活动信息 (返回字段参考voucherquota)
  56. * @apiSuccess {String} result.promotion_voucher_price 购买代金劵活动所需费用
  57. * @apiSuccess {String} result.voucher_storetimes_limit 每月最多可以发布的代金劵促销活动数量
  58. * @apiSuccess {Object} result.vouchertemplate_list 代金券模板列表 (返回字段参考vouchertemplate)
  59. * @apiSuccess {Int} result.page_total 总页数
  60. * @apiSuccess {Boolean} result.hasmore 是否有更多 true是false否
  61. */
  62. public function templatelist() {
  63. //检查过期的代金券模板状态设为失效
  64. $this->check_voucher_template_expire();
  65. $voucher_model = model('voucher');
  66. $isPlatformStore = false;
  67. $current_quota = false;
  68. if (!$this->store_info['is_platform_store']) {
  69. //查询是否存在可用套餐
  70. $current_quota = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
  71. }
  72. //查询列表
  73. $param = array();
  74. $param[] = array('vouchertemplate_store_id', '=', $this->store_info['store_id']);
  75. if (trim(input('param.txt_keyword'))) {
  76. $param[] = array('vouchertemplate_title', 'like', '%' . trim(input('param.txt_keyword')) . '%');
  77. }
  78. $select_state = intval(input('param.select_state'));
  79. if ($select_state) {
  80. $param[] = array('vouchertemplate_state', '=', $select_state);
  81. }
  82. if (input('param.txt_startdate')) {
  83. $param[] = array('vouchertemplate_enddate', '>=', strtotime(input('param.txt_startdate')));
  84. }
  85. if (input('param.txt_enddate')) {
  86. $param[] = array('vouchertemplate_startdate', '<=', strtotime(input('param.txt_enddate')));
  87. }
  88. $vouchertemplate_list_ = Db::name('vouchertemplate')->where($param)->order('vouchertemplate_id desc')->paginate(['list_rows' => 10, 'query' => request()->param()], false);
  89. $vouchertemplate_list = $vouchertemplate_list_->items();
  90. foreach ($vouchertemplate_list as $key => $val) {
  91. if (!$val['vouchertemplate_customimg']) {
  92. $vouchertemplate_list[$key]['vouchertemplate_customimg'] = ds_get_pic(ATTACH_COMMON,config('ds_config.default_goods_image'));
  93. } else {
  94. $vouchertemplate_list[$key]['vouchertemplate_customimg'] = ds_get_pic( ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'] , $val['vouchertemplate_customimg']);
  95. }
  96. }
  97. $result = array_merge(array('vouchertemplate_list' => $vouchertemplate_list, 'current_quota' => $current_quota, 'promotion_voucher_price' => config('ds_config.promotion_voucher_price'), 'voucher_storetimes_limit' => config('ds_config.voucher_storetimes_limit')), mobile_page($vouchertemplate_list_));
  98. ds_json_encode(10000, '', $result);
  99. }
  100. /**
  101. * @api {POST} api/Sellervoucher/quotaadd 购买套餐
  102. * @apiVersion 1.0.0
  103. * @apiGroup Sellervoucher
  104. *
  105. * @apiHeader {String} X-DS-KEY 卖家授权token
  106. *
  107. * @apiParam {Int} quota_quantity 购买数量
  108. *
  109. * @apiSuccess {String} code 返回码,10000为成功
  110. * @apiSuccess {String} message 返回消息
  111. * @apiSuccess {Object} result 返回数据
  112. * @apiSuccess {Int} result.voucherquota_endtime 代金券活动结束时间
  113. */
  114. public function quotaadd() {
  115. if (intval(config('ds_config.promotion_voucher_price')) == 0) {
  116. ds_json_encode(10001, lang('param_error'));
  117. }
  118. $quota_quantity = intval(input('post.quota_quantity'));
  119. if ($quota_quantity <= 0 || $quota_quantity > 12) {
  120. ds_json_encode(10001, lang('voucher_apply_num_error'));
  121. }
  122. //获取当前价格
  123. $current_price = intval(config('ds_config.promotion_voucher_price'));
  124. $voucher_model = model('voucher');
  125. //获取该用户已有套餐
  126. $current_quota = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
  127. $quota_add_time = 86400 * 30 * $quota_quantity;
  128. if (empty($current_quota)) {
  129. //生成套餐
  130. $param = array();
  131. $param['voucherquota_memberid'] = $this->seller_info['member_id'];
  132. $param['voucherquota_membername'] = $this->seller_info['seller_name'];
  133. $param['voucherquota_storeid'] = $this->store_info['store_id'];
  134. $param['voucherquota_storename'] = $this->store_info['store_name'];
  135. $param['voucherquota_starttime'] = TIMESTAMP;
  136. $param['voucherquota_endtime'] = TIMESTAMP + $quota_add_time;
  137. $param['voucherquota_state'] = 1;
  138. $reault = Db::name('voucherquota')->insert($param);
  139. } else {
  140. $param = array();
  141. $param['voucherquota_endtime'] = $current_quota['voucherquota_endtime'] + $quota_add_time;
  142. $reault = Db::name('voucherquota')->where(array('voucherquota_id' => $current_quota['voucherquota_id']))->update($param);
  143. }
  144. //记录店铺费用
  145. $this->recordStorecost($current_price * $quota_quantity, lang('buy_voucher_package'));
  146. $this->recordSellerlog(lang('buy') . $quota_quantity . lang('voucher_plan') . $current_price . lang('ds_yuan'));
  147. if ($reault) {
  148. ds_json_encode(10000, lang('voucher_apply_buy_succ'), array('voucherquota_endtime' => $param['voucherquota_endtime']));
  149. } else {
  150. ds_json_encode(10001, lang('ds_common_op_fail'));
  151. }
  152. }
  153. /**
  154. * @api {POST} api/Sellervoucher/templateadd 代金券模版添加
  155. * @apiVersion 1.0.0
  156. * @apiGroup Sellervoucher
  157. *
  158. * @apiHeader {String} X-DS-KEY 卖家授权token
  159. *
  160. * @apiParam {String} txt_template_title 代金券模版名称
  161. * @apiParam {String} txt_template_total 代金券总数
  162. * @apiParam {String} select_template_price 面额
  163. * @apiParam {String} txt_template_limit 订单限额
  164. * @apiParam {String} txt_template_describe 代金券模版描述
  165. * @apiParam {String} txt_template_enddate 有效期结束时间 YYYY-MM-DD
  166. * @apiParam {String} storeclass_id 所属店铺分类ID
  167. * @apiParam {String} eachlimit 每人限领张数
  168. *
  169. * @apiSuccess {String} code 返回码,10000为成功
  170. * @apiSuccess {String} message 返回消息
  171. * @apiSuccess {Object} result 返回数据
  172. */
  173. public function templateadd() {
  174. $voucher_model = model('voucher');
  175. $isPlatformStore = $this->store_info['is_platform_store'];
  176. $quotainfo = array();
  177. if (!$isPlatformStore) {
  178. //查询当前可以使用的套餐
  179. $quotainfo = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
  180. if (empty($quotainfo)) {
  181. if (intval(config('ds_config.promotion_voucher_price')) == 0) {
  182. $quotainfo = array('voucherquota_id' => 0, 'voucherquota_starttime' => TIMESTAMP, 'voucherquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  183. } else {
  184. ds_json_encode(10001, lang('voucher_template_quotanull'));
  185. }
  186. }
  187. //查询该套餐下代金券模板列表
  188. $count = Db::name('vouchertemplate')->where(array('vouchertemplate_quotaid' => $quotainfo['voucherquota_id'], 'vouchertemplate_state' => $this->templatestate_arr['usable'][0]))->count();
  189. if ($count >= config('ds_config.voucher_storetimes_limit')) {
  190. $message = sprintf(lang('voucher_template_noresidual'), config('ds_config.voucher_storetimes_limit'));
  191. ds_json_encode(10001, $message);
  192. }
  193. }
  194. $common_data = $this->common_data();
  195. $pricelist = $common_data['pricelist'];
  196. //验证提交的内容面额不能大于限额
  197. $data = [
  198. 'txt_template_title' => input('post.txt_template_title'),
  199. 'txt_template_total' => input('post.txt_template_total'),
  200. 'select_template_price' => input('post.select_template_price'),
  201. 'txt_template_limit' => input('post.txt_template_limit'),
  202. 'txt_template_describe' => input('post.txt_template_describe'),
  203. ];
  204. $sellervoucher_validate = ds_validate('sellervoucher');
  205. if (!$sellervoucher_validate->scene('templateadd')->check($data)) {
  206. ds_json_encode(10001, $sellervoucher_validate->getError());
  207. }
  208. //金额验证
  209. $price = intval(input('post.select_template_price')) > 0 ? intval(input('post.select_template_price')) : 0;
  210. foreach ($pricelist as $k => $v) {
  211. if ($v['voucherprice'] == $price) {
  212. $chooseprice = $v; //取得当前选择的面额记录
  213. }
  214. }
  215. if (empty($chooseprice)) {
  216. ds_json_encode(10001, lang('voucher_template_pricelisterror'));
  217. }
  218. $limit = intval(input('post.txt_template_limit')) > 0 ? intval(input('post.txt_template_limit')) : 0;
  219. if ($price >= $limit) {
  220. ds_json_encode(10001, lang('voucher_template_price_error'));
  221. }
  222. $insert_arr = array();
  223. $insert_arr['vouchertemplate_title'] = trim(input('post.txt_template_title'));
  224. $insert_arr['vouchertemplate_desc'] = trim(input('post.txt_template_describe'));
  225. $insert_arr['vouchertemplate_startdate'] = TIMESTAMP; //默认代金券模板的有效期为当前时间
  226. if (input('post.txt_template_enddate')) {
  227. $enddate = strtotime(input('post.txt_template_enddate'));
  228. if (!$isPlatformStore && $enddate > $quotainfo['voucherquota_endtime']) {
  229. $enddate = $quotainfo['voucherquota_endtime'];
  230. }
  231. $insert_arr['vouchertemplate_enddate'] = $enddate;
  232. } else {//如果没有添加有效期则默认为套餐的结束时间
  233. if ($isPlatformStore)
  234. $insert_arr['vouchertemplate_enddate'] = TIMESTAMP + 2592000; // 自营店 默认30天到期
  235. else
  236. $insert_arr['vouchertemplate_enddate'] = $quotainfo['voucherquota_endtime'];
  237. }
  238. $insert_arr['vouchertemplate_price'] = $price;
  239. $insert_arr['vouchertemplate_limit'] = $limit;
  240. $insert_arr['vouchertemplate_store_id'] = $this->store_info['store_id'];
  241. $insert_arr['vouchertemplate_storename'] = $this->store_info['store_name'];
  242. $insert_arr['vouchertemplate_sc_id'] = intval(input('post.storeclass_id'));
  243. $insert_arr['vouchertemplate_creator_id'] = $this->seller_info['member_id'];
  244. $insert_arr['vouchertemplate_state'] = $this->templatestate_arr['usable'][0];
  245. $insert_arr['vouchertemplate_total'] = intval(input('post.txt_template_total')) > 0 ? intval(input('post.txt_template_total')) : 0;
  246. $insert_arr['vouchertemplate_giveout'] = 0;
  247. $insert_arr['vouchertemplate_used'] = 0;
  248. $insert_arr['vouchertemplate_gettype'] = 1;
  249. $insert_arr['vouchertemplate_adddate'] = TIMESTAMP;
  250. $insert_arr['vouchertemplate_quotaid'] = isset($quotainfo['voucherquota_id']) ? $quotainfo['voucherquota_id'] : 0;
  251. $insert_arr['vouchertemplate_points'] = $chooseprice['voucherprice_defaultpoints'];
  252. $insert_arr['vouchertemplate_eachlimit'] = intval(input('post.eachlimit')) > 0 ? intval(input('post.eachlimit')) : 0;
  253. $insert_arr['vouchertemplate_if_private'] = intval(input('post.vouchertemplate_if_private'));
  254. //自定义图片
  255. if (!empty($_FILES['customimg']['name'])) {
  256. $file_name = $this->store_info['store_id'] . '_' . date('YmdHis') . rand(10000, 99999) . '.png';
  257. $res = ds_upload_pic(ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'], 'customimg', $file_name);
  258. if ($res['code']) {
  259. $file_name = $res['data']['file_name'];
  260. $insert_arr['vouchertemplate_customimg'] = $file_name;
  261. } else {
  262. ds_json_encode(10001, $res['msg']);
  263. }
  264. }
  265. $rs = Db::name('vouchertemplate')->insert($insert_arr);
  266. if ($rs) {
  267. ds_json_encode(10000, lang('ds_common_op_succ'));
  268. } else {
  269. ds_json_encode(10001, lang('ds_common_op_fail'));
  270. }
  271. }
  272. public function common_data() {
  273. $voucher_model = model('voucher');
  274. //查询面额列表
  275. $pricelist = Db::name('voucherprice')->order('voucherprice asc')->select()->toArray();
  276. $quotainfo = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
  277. $quotainfo['voucherquota_endtime'] = date('Y-m-d',$quotainfo['voucherquota_endtime']);
  278. if (empty($pricelist)) {
  279. ds_json_encode(10001, lang('voucher_template_pricelisterror'));
  280. }
  281. return array('pricelist' => $pricelist,'quotainfo'=>$quotainfo, 'voucher_buyertimes_limit' => config('ds_config.voucher_buyertimes_limit'));
  282. }
  283. /**
  284. * @api {POST} api/Sellervoucher/get_common_data 代金券新增/编辑公共数据
  285. * @apiVersion 1.0.0
  286. * @apiGroup Sellervoucher
  287. *
  288. * @apiHeader {String} X-DS-KEY 卖家授权token
  289. *
  290. * @apiSuccess {String} code 返回码,10000为成功
  291. * @apiSuccess {String} message 返回消息
  292. * @apiSuccess {Object} result 返回数据
  293. * @apiSuccess {Object[]} result.pricelist 面额列表
  294. * @apiSuccess {Object[]} result.pricelist.voucherprice 面额
  295. * @apiSuccess {Object[]} result.pricelist.voucherprice_defaultpoints 兑换积分
  296. * @apiSuccess {Object[]} result.pricelist.voucherprice_describe 描述
  297. * @apiSuccess {Object[]} result.pricelist.voucherprice_id 面额ID
  298. * @apiSuccess {String} result.voucher_buyertimes_limit 买家最多只能拥有同一个店铺尚未消费抵用的店铺代金券最大数量
  299. */
  300. public function get_common_data() {
  301. $common_data = $this->common_data();
  302. ds_json_encode(10000, '', $common_data);
  303. }
  304. /**
  305. * @api {POST} api/Sellervoucher/templateadd 代金券模版编辑
  306. * @apiVersion 1.0.0
  307. * @apiGroup Sellervoucher
  308. *
  309. * @apiHeader {String} X-DS-KEY 卖家授权token
  310. *
  311. * @apiParam {Int} tid 代金券模版ID
  312. * @apiParam {String} txt_template_title 代金券模版名称
  313. * @apiParam {String} txt_template_total 代金券总数
  314. * @apiParam {String} select_template_price 面额
  315. * @apiParam {String} txt_template_limit 订单限额
  316. * @apiParam {String} txt_template_describe 代金券模版描述
  317. * @apiParam {String} txt_template_enddate 有效期结束时间 YYYY-MM-DD
  318. * @apiParam {String} storeclass_id 所属店铺分类ID
  319. * @apiParam {String} eachlimit 每人限领张数
  320. * @apiParam {Int} tstate 代金券模版状态 1:有效 2:失效
  321. *
  322. * @apiSuccess {String} code 返回码,10000为成功
  323. * @apiSuccess {String} message 返回消息
  324. * @apiSuccess {Object} result 返回数据
  325. */
  326. public function templateedit() {
  327. $t_id = intval(input('param.tid'));
  328. if ($t_id <= 0) {
  329. ds_json_encode(10001, lang('param_error'));
  330. }
  331. //查询模板信息
  332. $param = array();
  333. $param[] = array('vouchertemplate_id', '=', $t_id);
  334. $param[] = array('vouchertemplate_store_id', '=', $this->store_info['store_id']);
  335. $param[] = array('vouchertemplate_state', '=', $this->templatestate_arr['usable'][0]);
  336. $param[] = array('vouchertemplate_giveout', '<=', '0');
  337. $param[] = array('vouchertemplate_enddate', '>', TIMESTAMP);
  338. $t_info = Db::name('vouchertemplate')->where($param)->find();
  339. if (empty($t_info)) {
  340. ds_json_encode(10001, lang('param_error'));
  341. }
  342. $isPlatformStore = $this->store_info['is_platform_store'];
  343. View::assign('isPlatformStore', $isPlatformStore);
  344. $quotainfo = array();
  345. if (!$isPlatformStore) {
  346. //查询套餐信息
  347. $quotainfo = Db::name('voucherquota')->where(array(
  348. 'voucherquota_id' => $t_info['vouchertemplate_quotaid'],
  349. 'voucherquota_storeid' => $this->store_info['store_id']
  350. ))->find();
  351. if (empty($quotainfo)) {
  352. ds_json_encode(10001, lang('voucher_template_quotanull'));
  353. }
  354. }
  355. $common_data = $this->common_data();
  356. $pricelist = $common_data['pricelist'];
  357. //验证提交的内容面额不能大于限额
  358. $data = [
  359. 'txt_template_title' => input('post.txt_template_title'),
  360. 'txt_template_total' => input('post.txt_template_total'),
  361. 'select_template_price' => input('post.select_template_price'),
  362. 'txt_template_limit' => input('post.txt_template_limit'),
  363. 'txt_template_describe' => input('post.txt_template_describe'),
  364. ];
  365. $sellervoucher_validate = ds_validate('sellervoucher');
  366. if (!$sellervoucher_validate->scene('templateedit')->check($data)) {
  367. ds_json_encode(10001, $sellervoucher_validate->getError());
  368. }
  369. //金额验证
  370. $price = intval(input('post.select_template_price')) > 0 ? intval(input('post.select_template_price')) : 0;
  371. foreach ($pricelist as $k => $v) {
  372. if ($v['voucherprice'] == $price) {
  373. $chooseprice = $v; //取得当前选择的面额记录
  374. }
  375. }
  376. if (empty($chooseprice)) {
  377. ds_json_encode(10001, lang('voucher_template_pricelisterror'));
  378. }
  379. $limit = intval(input('post.txt_template_limit')) > 0 ? intval(input('post.txt_template_limit')) : 0;
  380. if ($price >= $limit) {
  381. ds_json_encode(10001, lang('voucher_template_price_error'));
  382. }
  383. $update_arr = array();
  384. $update_arr['vouchertemplate_title'] = trim(input('post.txt_template_title'));
  385. $update_arr['vouchertemplate_desc'] = trim(input('post.txt_template_describe'));
  386. if (input('post.txt_template_enddate')) {
  387. $enddate = strtotime(input('post.txt_template_enddate'));
  388. if (!$isPlatformStore && $enddate > $quotainfo['voucherquota_endtime']) {
  389. $enddate = $quotainfo['voucherquota_endtime'];
  390. }
  391. $update_arr['vouchertemplate_enddate'] = $enddate;
  392. } else {//如果没有添加有效期则默认为套餐的结束时间
  393. if ($isPlatformStore)
  394. $update_arr['vouchertemplate_enddate'] = TIMESTAMP + 2592000; // 自营店 默认30天到期
  395. else
  396. $update_arr['vouchertemplate_enddate'] = $quotainfo['voucherquota_endtime'];
  397. }
  398. $update_arr['vouchertemplate_price'] = $price;
  399. $update_arr['vouchertemplate_limit'] = $limit;
  400. $update_arr['vouchertemplate_sc_id'] = intval(input('post.storeclass_id'));
  401. $update_arr['vouchertemplate_state'] = intval(input('post.tstate')) == $this->templatestate_arr['usable'][0] ? $this->templatestate_arr['usable'][0] : $this->templatestate_arr['disabled'][0];
  402. $update_arr['vouchertemplate_total'] = intval(input('post.txt_template_total')) > 0 ? intval(input('post.txt_template_total')) : 0;
  403. $update_arr['vouchertemplate_points'] = $chooseprice['voucherprice_defaultpoints'];
  404. $update_arr['vouchertemplate_eachlimit'] = intval(input('post.eachlimit')) > 0 ? intval(input('post.eachlimit')) : 0;
  405. $update_arr['vouchertemplate_if_private'] = intval(input('post.vouchertemplate_if_private'));
  406. //自定义图片
  407. if (!empty($_FILES['customimg']['name'])) {
  408. $file_name = $this->store_info['store_id'] . '_' . date('YmdHis') . rand(10000, 99999);
  409. $res = ds_upload_pic(ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'], 'customimg', $file_name);
  410. if ($res['code']) {
  411. $file_name = $res['data']['file_name'];
  412. //删除原图
  413. if (!empty($t_info['vouchertemplate_customimg'])) {//如果模板存在,则删除原模板图片
  414. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'] . DIRECTORY_SEPARATOR . $t_info['vouchertemplate_customimg']);
  415. }
  416. $update_arr['vouchertemplate_customimg'] = $file_name;
  417. } else {
  418. ds_json_encode(10001, $res['msg']);
  419. }
  420. }
  421. $rs = Db::name('vouchertemplate')->where(array('vouchertemplate_id' => $t_info['vouchertemplate_id']))->update($update_arr);
  422. if ($rs) {
  423. ds_json_encode(10000, lang('ds_common_op_succ'));
  424. } else {
  425. ds_json_encode(10001, lang('ds_common_op_fail'));
  426. }
  427. }
  428. /**
  429. * @api {POST} api/Sellervoucher/templatedel 删除代金券
  430. * @apiVersion 1.0.0
  431. * @apiGroup Sellervoucher
  432. *
  433. * @apiHeader {String} X-DS-KEY 卖家授权token
  434. *
  435. * @apiParam {Int} tid 代金券模版ID
  436. *
  437. * @apiSuccess {String} code 返回码,10000为成功
  438. * @apiSuccess {String} message 返回消息
  439. * @apiSuccess {Object} result 返回数据
  440. */
  441. public function templatedel() {
  442. $t_id = intval(input('param.tid'));
  443. if ($t_id <= 0) {
  444. ds_json_encode(10001, lang('param_error'));
  445. }
  446. //查询模板信息
  447. $param = array();
  448. $param[] = array('vouchertemplate_id', '=', $t_id);
  449. $param[] = array('vouchertemplate_store_id', '=', $this->store_info['store_id']);
  450. $param[] = array('vouchertemplate_giveout', '<=', '0'); //会员没领取过代金券才可删除
  451. $t_info = Db::name('vouchertemplate')->where($param)->find();
  452. if (empty($t_info)) {
  453. ds_json_encode(10001, lang('param_error'));
  454. }
  455. $rs = Db::name('vouchertemplate')->where(array('vouchertemplate_id' => $t_info['vouchertemplate_id']))->delete();
  456. if ($rs) {
  457. //删除自定义的图片
  458. if (trim($t_info['vouchertemplate_customimg'])) {
  459. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'] . DIRECTORY_SEPARATOR . $t_info['vouchertemplate_customimg']);
  460. }
  461. ds_json_encode(10000, lang('ds_common_del_succ'));
  462. } else {
  463. ds_json_encode(10001, lang('ds_common_del_fail'));
  464. }
  465. }
  466. /**
  467. * 查看代金券详细
  468. */
  469. public function templateinfo() {
  470. $t_id = intval(input('param.tid'));
  471. if ($t_id <= 0) {
  472. ds_json_encode(10001, lang('param_error'), 'Sellervoucher/templatelist');
  473. }
  474. //查询模板信息
  475. $param = array();
  476. $param['vouchertemplate_id'] = $t_id;
  477. $param['vouchertemplate_store_id'] = $this->store_info['store_id'];
  478. $t_info = Db::name('vouchertemplate')->where($param)->find();
  479. ds_json_encode(10000, lang('ds_common_del_succ'), array('t_info' => $t_info));
  480. }
  481. /*
  482. * 把代金券模版设为失效
  483. */
  484. private function check_voucher_template_expire($voucher_template_id = '') {
  485. $where_array = array();
  486. if (empty($voucher_template_id)) {
  487. $where_array[] = array('vouchertemplate_enddate', '<', TIMESTAMP);
  488. } else {
  489. $where_array[] = array('vouchertemplate_id', '=', $voucher_template_id);
  490. }
  491. $where_array[] = array('vouchertemplate_state', '=', $this->templatestate_arr['usable'][0]);
  492. Db::name('vouchertemplate')->where($where_array)->update(array('vouchertemplate_state' => $this->templatestate_arr['disabled'][0]));
  493. }
  494. }