123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
- <?php
- namespace app\api\controller;
- use think\facade\View;
- use think\facade\Lang;
- use think\facade\Db;
- class Sellervoucher extends MobileSeller {
- private $quotastate_arr;
- private $templatestate_arr;
- public function initialize() {
- parent::initialize();
- Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellervoucher.lang.php');
- if (config('ds_config.voucher_allow') != 1) {
- ds_json_encode(10001, lang('voucher_unavailable'));
- }
-
- $this->quotastate_arr = array(
- 'activity' => array(1, lang('voucher_quotastate_activity')),
- 'cancel' => array(2, lang('voucher_quotastate_cancel')),
- 'expire' => array(3, lang('voucher_quotastate_expire'))
- );
-
- $this->templatestate_arr = array(
- 'usable' => array(1, lang('voucher_templatestate_usable')),
- 'disabled' => array(2, lang('voucher_templatestate_disabled'))
- );
- }
-
- public function templatelist() {
-
- $this->check_voucher_template_expire();
- $voucher_model = model('voucher');
- $isPlatformStore = false;
- $current_quota = false;
- if (!$this->store_info['is_platform_store']) {
-
- $current_quota = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
- }
-
- $param = array();
- $param[] = array('vouchertemplate_store_id', '=', $this->store_info['store_id']);
- if (trim(input('param.txt_keyword'))) {
- $param[] = array('vouchertemplate_title', 'like', '%' . trim(input('param.txt_keyword')) . '%');
- }
- $select_state = intval(input('param.select_state'));
- if ($select_state) {
- $param[] = array('vouchertemplate_state', '=', $select_state);
- }
- if (input('param.txt_startdate')) {
- $param[] = array('vouchertemplate_enddate', '>=', strtotime(input('param.txt_startdate')));
- }
- if (input('param.txt_enddate')) {
- $param[] = array('vouchertemplate_startdate', '<=', strtotime(input('param.txt_enddate')));
- }
- $vouchertemplate_list_ = Db::name('vouchertemplate')->where($param)->order('vouchertemplate_id desc')->paginate(['list_rows' => 10, 'query' => request()->param()], false);
- $vouchertemplate_list = $vouchertemplate_list_->items();
- foreach ($vouchertemplate_list as $key => $val) {
- if (!$val['vouchertemplate_customimg']) {
- $vouchertemplate_list[$key]['vouchertemplate_customimg'] = ds_get_pic(ATTACH_COMMON,config('ds_config.default_goods_image'));
- } else {
- $vouchertemplate_list[$key]['vouchertemplate_customimg'] = ds_get_pic( ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'] , $val['vouchertemplate_customimg']);
- }
- }
- $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_));
- ds_json_encode(10000, '', $result);
- }
-
- public function quotaadd() {
- if (intval(config('ds_config.promotion_voucher_price')) == 0) {
- ds_json_encode(10001, lang('param_error'));
- }
- $quota_quantity = intval(input('post.quota_quantity'));
- if ($quota_quantity <= 0 || $quota_quantity > 12) {
- ds_json_encode(10001, lang('voucher_apply_num_error'));
- }
-
- $current_price = intval(config('ds_config.promotion_voucher_price'));
- $voucher_model = model('voucher');
-
- $current_quota = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
- $quota_add_time = 86400 * 30 * $quota_quantity;
- if (empty($current_quota)) {
-
- $param = array();
- $param['voucherquota_memberid'] = $this->seller_info['member_id'];
- $param['voucherquota_membername'] = $this->seller_info['seller_name'];
- $param['voucherquota_storeid'] = $this->store_info['store_id'];
- $param['voucherquota_storename'] = $this->store_info['store_name'];
- $param['voucherquota_starttime'] = TIMESTAMP;
- $param['voucherquota_endtime'] = TIMESTAMP + $quota_add_time;
- $param['voucherquota_state'] = 1;
- $reault = Db::name('voucherquota')->insert($param);
- } else {
- $param = array();
- $param['voucherquota_endtime'] = $current_quota['voucherquota_endtime'] + $quota_add_time;
- $reault = Db::name('voucherquota')->where(array('voucherquota_id' => $current_quota['voucherquota_id']))->update($param);
- }
-
- $this->recordStorecost($current_price * $quota_quantity, lang('buy_voucher_package'));
- $this->recordSellerlog(lang('buy') . $quota_quantity . lang('voucher_plan') . $current_price . lang('ds_yuan'));
- if ($reault) {
- ds_json_encode(10000, lang('voucher_apply_buy_succ'), array('voucherquota_endtime' => $param['voucherquota_endtime']));
- } else {
- ds_json_encode(10001, lang('ds_common_op_fail'));
- }
- }
-
- public function templateadd() {
- $voucher_model = model('voucher');
- $isPlatformStore = $this->store_info['is_platform_store'];
- $quotainfo = array();
- if (!$isPlatformStore) {
-
- $quotainfo = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
- if (empty($quotainfo)) {
- if (intval(config('ds_config.promotion_voucher_price')) == 0) {
- $quotainfo = array('voucherquota_id' => 0, 'voucherquota_starttime' => TIMESTAMP, 'voucherquota_endtime' => TIMESTAMP + 86400 * 30);
- } else {
- ds_json_encode(10001, lang('voucher_template_quotanull'));
- }
- }
-
- $count = Db::name('vouchertemplate')->where(array('vouchertemplate_quotaid' => $quotainfo['voucherquota_id'], 'vouchertemplate_state' => $this->templatestate_arr['usable'][0]))->count();
- if ($count >= config('ds_config.voucher_storetimes_limit')) {
- $message = sprintf(lang('voucher_template_noresidual'), config('ds_config.voucher_storetimes_limit'));
- ds_json_encode(10001, $message);
- }
- }
- $common_data = $this->common_data();
- $pricelist = $common_data['pricelist'];
-
- $data = [
- 'txt_template_title' => input('post.txt_template_title'),
- 'txt_template_total' => input('post.txt_template_total'),
- 'select_template_price' => input('post.select_template_price'),
- 'txt_template_limit' => input('post.txt_template_limit'),
- 'txt_template_describe' => input('post.txt_template_describe'),
- ];
- $sellervoucher_validate = ds_validate('sellervoucher');
- if (!$sellervoucher_validate->scene('templateadd')->check($data)) {
- ds_json_encode(10001, $sellervoucher_validate->getError());
- }
-
- $price = intval(input('post.select_template_price')) > 0 ? intval(input('post.select_template_price')) : 0;
- foreach ($pricelist as $k => $v) {
- if ($v['voucherprice'] == $price) {
- $chooseprice = $v;
- }
- }
- if (empty($chooseprice)) {
- ds_json_encode(10001, lang('voucher_template_pricelisterror'));
- }
- $limit = intval(input('post.txt_template_limit')) > 0 ? intval(input('post.txt_template_limit')) : 0;
- if ($price >= $limit) {
- ds_json_encode(10001, lang('voucher_template_price_error'));
- }
- $insert_arr = array();
- $insert_arr['vouchertemplate_title'] = trim(input('post.txt_template_title'));
- $insert_arr['vouchertemplate_desc'] = trim(input('post.txt_template_describe'));
- $insert_arr['vouchertemplate_startdate'] = TIMESTAMP;
- if (input('post.txt_template_enddate')) {
- $enddate = strtotime(input('post.txt_template_enddate'));
- if (!$isPlatformStore && $enddate > $quotainfo['voucherquota_endtime']) {
- $enddate = $quotainfo['voucherquota_endtime'];
- }
- $insert_arr['vouchertemplate_enddate'] = $enddate;
- } else {
- if ($isPlatformStore)
- $insert_arr['vouchertemplate_enddate'] = TIMESTAMP + 2592000;
- else
- $insert_arr['vouchertemplate_enddate'] = $quotainfo['voucherquota_endtime'];
- }
- $insert_arr['vouchertemplate_price'] = $price;
- $insert_arr['vouchertemplate_limit'] = $limit;
- $insert_arr['vouchertemplate_store_id'] = $this->store_info['store_id'];
- $insert_arr['vouchertemplate_storename'] = $this->store_info['store_name'];
- $insert_arr['vouchertemplate_sc_id'] = intval(input('post.storeclass_id'));
- $insert_arr['vouchertemplate_creator_id'] = $this->seller_info['member_id'];
- $insert_arr['vouchertemplate_state'] = $this->templatestate_arr['usable'][0];
- $insert_arr['vouchertemplate_total'] = intval(input('post.txt_template_total')) > 0 ? intval(input('post.txt_template_total')) : 0;
- $insert_arr['vouchertemplate_giveout'] = 0;
- $insert_arr['vouchertemplate_used'] = 0;
- $insert_arr['vouchertemplate_gettype'] = 1;
- $insert_arr['vouchertemplate_adddate'] = TIMESTAMP;
- $insert_arr['vouchertemplate_quotaid'] = isset($quotainfo['voucherquota_id']) ? $quotainfo['voucherquota_id'] : 0;
- $insert_arr['vouchertemplate_points'] = $chooseprice['voucherprice_defaultpoints'];
- $insert_arr['vouchertemplate_eachlimit'] = intval(input('post.eachlimit')) > 0 ? intval(input('post.eachlimit')) : 0;
- $insert_arr['vouchertemplate_if_private'] = intval(input('post.vouchertemplate_if_private'));
-
- if (!empty($_FILES['customimg']['name'])) {
- $file_name = $this->store_info['store_id'] . '_' . date('YmdHis') . rand(10000, 99999) . '.png';
- $res = ds_upload_pic(ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'], 'customimg', $file_name);
- if ($res['code']) {
- $file_name = $res['data']['file_name'];
- $insert_arr['vouchertemplate_customimg'] = $file_name;
- } else {
- ds_json_encode(10001, $res['msg']);
- }
- }
- $rs = Db::name('vouchertemplate')->insert($insert_arr);
- if ($rs) {
- ds_json_encode(10000, lang('ds_common_op_succ'));
- } else {
- ds_json_encode(10001, lang('ds_common_op_fail'));
- }
- }
- public function common_data() {
- $voucher_model = model('voucher');
-
- $pricelist = Db::name('voucherprice')->order('voucherprice asc')->select()->toArray();
- $quotainfo = $voucher_model->getVoucherquotaCurrent($this->store_info['store_id']);
- $quotainfo['voucherquota_endtime'] = date('Y-m-d',$quotainfo['voucherquota_endtime']);
- if (empty($pricelist)) {
- ds_json_encode(10001, lang('voucher_template_pricelisterror'));
- }
- return array('pricelist' => $pricelist,'quotainfo'=>$quotainfo, 'voucher_buyertimes_limit' => config('ds_config.voucher_buyertimes_limit'));
- }
-
- public function get_common_data() {
- $common_data = $this->common_data();
- ds_json_encode(10000, '', $common_data);
- }
-
- public function templateedit() {
- $t_id = intval(input('param.tid'));
- if ($t_id <= 0) {
- ds_json_encode(10001, lang('param_error'));
- }
-
- $param = array();
- $param[] = array('vouchertemplate_id', '=', $t_id);
- $param[] = array('vouchertemplate_store_id', '=', $this->store_info['store_id']);
- $param[] = array('vouchertemplate_state', '=', $this->templatestate_arr['usable'][0]);
- $param[] = array('vouchertemplate_giveout', '<=', '0');
- $param[] = array('vouchertemplate_enddate', '>', TIMESTAMP);
- $t_info = Db::name('vouchertemplate')->where($param)->find();
- if (empty($t_info)) {
- ds_json_encode(10001, lang('param_error'));
- }
- $isPlatformStore = $this->store_info['is_platform_store'];
- View::assign('isPlatformStore', $isPlatformStore);
- $quotainfo = array();
- if (!$isPlatformStore) {
-
- $quotainfo = Db::name('voucherquota')->where(array(
- 'voucherquota_id' => $t_info['vouchertemplate_quotaid'],
- 'voucherquota_storeid' => $this->store_info['store_id']
- ))->find();
- if (empty($quotainfo)) {
- ds_json_encode(10001, lang('voucher_template_quotanull'));
- }
- }
- $common_data = $this->common_data();
- $pricelist = $common_data['pricelist'];
-
- $data = [
- 'txt_template_title' => input('post.txt_template_title'),
- 'txt_template_total' => input('post.txt_template_total'),
- 'select_template_price' => input('post.select_template_price'),
- 'txt_template_limit' => input('post.txt_template_limit'),
- 'txt_template_describe' => input('post.txt_template_describe'),
- ];
- $sellervoucher_validate = ds_validate('sellervoucher');
- if (!$sellervoucher_validate->scene('templateedit')->check($data)) {
- ds_json_encode(10001, $sellervoucher_validate->getError());
- }
-
- $price = intval(input('post.select_template_price')) > 0 ? intval(input('post.select_template_price')) : 0;
- foreach ($pricelist as $k => $v) {
- if ($v['voucherprice'] == $price) {
- $chooseprice = $v;
- }
- }
- if (empty($chooseprice)) {
- ds_json_encode(10001, lang('voucher_template_pricelisterror'));
- }
- $limit = intval(input('post.txt_template_limit')) > 0 ? intval(input('post.txt_template_limit')) : 0;
- if ($price >= $limit) {
- ds_json_encode(10001, lang('voucher_template_price_error'));
- }
- $update_arr = array();
- $update_arr['vouchertemplate_title'] = trim(input('post.txt_template_title'));
- $update_arr['vouchertemplate_desc'] = trim(input('post.txt_template_describe'));
- if (input('post.txt_template_enddate')) {
- $enddate = strtotime(input('post.txt_template_enddate'));
- if (!$isPlatformStore && $enddate > $quotainfo['voucherquota_endtime']) {
- $enddate = $quotainfo['voucherquota_endtime'];
- }
- $update_arr['vouchertemplate_enddate'] = $enddate;
- } else {
- if ($isPlatformStore)
- $update_arr['vouchertemplate_enddate'] = TIMESTAMP + 2592000;
- else
- $update_arr['vouchertemplate_enddate'] = $quotainfo['voucherquota_endtime'];
- }
- $update_arr['vouchertemplate_price'] = $price;
- $update_arr['vouchertemplate_limit'] = $limit;
- $update_arr['vouchertemplate_sc_id'] = intval(input('post.storeclass_id'));
- $update_arr['vouchertemplate_state'] = intval(input('post.tstate')) == $this->templatestate_arr['usable'][0] ? $this->templatestate_arr['usable'][0] : $this->templatestate_arr['disabled'][0];
- $update_arr['vouchertemplate_total'] = intval(input('post.txt_template_total')) > 0 ? intval(input('post.txt_template_total')) : 0;
- $update_arr['vouchertemplate_points'] = $chooseprice['voucherprice_defaultpoints'];
- $update_arr['vouchertemplate_eachlimit'] = intval(input('post.eachlimit')) > 0 ? intval(input('post.eachlimit')) : 0;
- $update_arr['vouchertemplate_if_private'] = intval(input('post.vouchertemplate_if_private'));
-
- if (!empty($_FILES['customimg']['name'])) {
- $file_name = $this->store_info['store_id'] . '_' . date('YmdHis') . rand(10000, 99999);
- $res = ds_upload_pic(ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'], 'customimg', $file_name);
- if ($res['code']) {
- $file_name = $res['data']['file_name'];
-
- if (!empty($t_info['vouchertemplate_customimg'])) {
- @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'] . DIRECTORY_SEPARATOR . $t_info['vouchertemplate_customimg']);
- }
- $update_arr['vouchertemplate_customimg'] = $file_name;
- } else {
- ds_json_encode(10001, $res['msg']);
- }
- }
- $rs = Db::name('vouchertemplate')->where(array('vouchertemplate_id' => $t_info['vouchertemplate_id']))->update($update_arr);
- if ($rs) {
- ds_json_encode(10000, lang('ds_common_op_succ'));
- } else {
- ds_json_encode(10001, lang('ds_common_op_fail'));
- }
- }
-
- public function templatedel() {
- $t_id = intval(input('param.tid'));
- if ($t_id <= 0) {
- ds_json_encode(10001, lang('param_error'));
- }
-
- $param = array();
- $param[] = array('vouchertemplate_id', '=', $t_id);
- $param[] = array('vouchertemplate_store_id', '=', $this->store_info['store_id']);
- $param[] = array('vouchertemplate_giveout', '<=', '0');
- $t_info = Db::name('vouchertemplate')->where($param)->find();
- if (empty($t_info)) {
- ds_json_encode(10001, lang('param_error'));
- }
- $rs = Db::name('vouchertemplate')->where(array('vouchertemplate_id' => $t_info['vouchertemplate_id']))->delete();
- if ($rs) {
-
- if (trim($t_info['vouchertemplate_customimg'])) {
- @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_VOUCHER . DIRECTORY_SEPARATOR . $this->store_info['store_id'] . DIRECTORY_SEPARATOR . $t_info['vouchertemplate_customimg']);
- }
- ds_json_encode(10000, lang('ds_common_del_succ'));
- } else {
- ds_json_encode(10001, lang('ds_common_del_fail'));
- }
- }
-
- public function templateinfo() {
- $t_id = intval(input('param.tid'));
- if ($t_id <= 0) {
- ds_json_encode(10001, lang('param_error'), 'Sellervoucher/templatelist');
- }
-
- $param = array();
- $param['vouchertemplate_id'] = $t_id;
- $param['vouchertemplate_store_id'] = $this->store_info['store_id'];
- $t_info = Db::name('vouchertemplate')->where($param)->find();
- ds_json_encode(10000, lang('ds_common_del_succ'), array('t_info' => $t_info));
- }
-
- private function check_voucher_template_expire($voucher_template_id = '') {
- $where_array = array();
- if (empty($voucher_template_id)) {
- $where_array[] = array('vouchertemplate_enddate', '<', TIMESTAMP);
- } else {
- $where_array[] = array('vouchertemplate_id', '=', $voucher_template_id);
- }
- $where_array[] = array('vouchertemplate_state', '=', $this->templatestate_arr['usable'][0]);
- Db::name('vouchertemplate')->where($where_array)->update(array('vouchertemplate_state' => $this->templatestate_arr['disabled'][0]));
- }
- }
|